#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <math.h>
#include "type.h"
#include "cli_main.h"
#include "xml_conf.h"
#include "dataHandle.h"
#include "cmd.h"
#include "./../3rdparty/include/json-c/json.h"
typedef struct json_object * JSON;
extern I8 dev_mac[32];
static I8 *devNumber 	= "devNumber";
static I8 *devName 		= "devName";
static I8 *typeModel 	= "typeModel";
static I8 *phoneNumber 	= "phoneNumber";
static I8 *devPos 		= "devPos";
static I8 *height 		= "height";
static I8 *longitude 	= "longitude";
static I8 *latitude 	= "latitude";
static I8 *altitude    = "altitude";
static I8 *devAddresss 	= "devAddresss";
static I8 *timestamp 	= "timestamp";
static I8 *devType 		= "devType";
static I8 *devconf 		= "devConformation";
static I8 *paMaxPower   = "paMaxPower";
static I8 *patemp 		= "patemp";

static I8 *devtemp 		= "devtemp";
static I8 *boardtemp 	= "boardtemp";
static I8 *wireless_s 	= "wirelessSwitch";
static I8 *sms_s 		= "smsSwitch";

static I8 *pa_s 		= "paSwitch";

static I8 *sniffer_s 	= "autoSniffer";
static I8 *cpuload 		= "cpu";
static I8 *memload 		= "memory";
static I8 *uptime 		= "uptime";
static I8 *stat_string 	= "stats";
static I8 *type_string 	= "type";
static I8 *code_string 	= "code";
static I8 *data_string 	= "data";
static I8 *action       = "action";
static I8 *Cell_Number  = "cellNumber";

static I8 *Bts_band 	= "band";
static I8 *Bts_BCC 		= "bcc";
static I8 *Bts_MCC 		= "mcc";
static I8 *Bts_MNC 		= "mnc";
static I8 *Bts_LAC 		= "lac";
static I8 *Bts_NEI 		= "neighborsNumToSend";

static I8 *pawr         = "paWaveRatio";
//static I8 *pa_wr         = "waveRadio";
static I8 *pa_num       = "paNumber";
static I8 *PaPower      = "paPower";

static I8 *Cell_band    = "band";
static I8 *Cell_freq    = "freq";
static I8 *Cell_pci     = "pci";
static I8 *Cell_plmnid  = "plmnid";
static I8 *Cell_tac     = "tac";
static I8 *Cell_rssi    = "rssi";

static I8 *sr_cycle     = "cycle";
static I8 *operators    = "operators";
static I8 *formatType   = "formatType";
static I8 *tac_cycle    = "tacUpdateCycle";

static I8 *cfgcode      = "cfgcode";
static I8 *searchtime   = "searchtime";

static I8 *cm           = "CHINA_MOBILE";
static I8 *cu           = "CHINA_UNICOM";
static I8 *ct           = "CHINA_TELECOM";

static I8 *palist       = "palist";

static I8 *rruNum       = "rruNumber";
static I8 *paNum        = "paNumber";

static I8 *updatacount  = "updataCounts";
static I8 *serial_num   = "serialNumber";
static I8 *syncMode     = "syncmethod"; 
static I8 *syncStatus   = "syncstatus";  
static I8 *syncInfo     = "syncInfo";    
static I8 *temp         = "temp";
//static I8 *deviceNum     = "deviceNumber";
static I32 config_init  = 0;  



static I8 *delay_string = "ac_delay";
static I8 *upresult 	= "upresult";

static I8 *snf_BccMode 	= "autoBccMode";
static I8 *snf_isAuto 	= "isAuto";
static I8 *snf_runtime  = "runTime";
static I8 *snf_hour 	= "hour";
static I8 *snf_min 		= "minute";
static I8 *snf_sec 		= "second";
static I8 *num_true 	= "true";
static I8 *num_false 	= "false";
static I8 *auth_ip		= "authSrvHost";
static I8 *auth_port 	= "authPort";
static I8 *mng_ip 		= "mgr1SrvHost";
static I8 *mng_port 	= "mgr1Port";
static I8 *data_ip 		= "up1SrvHost";
static I8 *data_port 	= "up1Port";
static I8 *status_itvl 	= "statsCycSeconds";
static I8 *data_itvl 	= "dataUpCycSeconds";
static I8 *cap_itvl 	= "reDetectMinuts";
static I8 *save_time 	= "savetime";
static I8 *updata_url 	= "patchUrl";
static I8 *update_md5 	= "md5sum";
static I8 *patchInfo    = "patchInfo";
static I8 *startTime    = "startTime";
static I8 *endTime      = "endTime";
static I8 *message      = "message";
static I8 *imsi_s       = "imsi";
static I8 *imei_s       = "imei";
static I8 *tmsi_s       = "tmsi";
static I8 *smsMgrKey    = "smsMgrKey";

//gps
static I8 *rlt_string   = "result";
static I8 *msg_string   = "msg";


static I8 *GpsInfo      ="GpsInfo";

const I8 *ntpServerFile = "/etc/config/system";

static I8 imsilist[6][16] = {{0}, {0}, {0}, {0}, {0}, {0}};
static bool isRoot = false;
typedef struct {
    I8 startDate[12];
    I8 endDate[12];
} SR_DATE;
extern COMMANDLIST cmdList;

static void json_add_string(char * json_name, char * json_para, JSON json_root)
{
    JSON add_string;
    add_string = json_object_new_string(json_para);
    json_object_object_add(json_root, json_name, add_string);
}
static void json_add_int(char * json_name, int json_para, JSON json_root)
{ 
    JSON add_int;
    add_int = json_object_new_int(json_para);
    json_object_object_add(json_root, json_name, add_int);
}
static void json_add_double(char *json_name, double json_para, JSON json_root)
{
    JSON add_double;
    add_double = json_object_new_double(json_para);
    json_object_object_add(json_root, json_name, add_double);	

}

static void json_add_bool(char *json_name, bool json_para, JSON json_root)
{
    JSON add_double;
    add_double = json_object_new_boolean(json_para);
    json_object_object_add(json_root, json_name, add_double);	

}

static void json_add_int_array(I8 *json_name, I32 para, JSON  root)
{
     JSON child_node;
     child_node = json_object_new_object();
  
     json_add_int(json_name, para, child_node);

     json_object_array_add(root, child_node);
}


static void json_add_array(I8 *json_name, I8 *para, JSON  root)
{
     JSON child_node;
     child_node = json_object_new_object();
  
     json_add_string(json_name, para, child_node);

     json_object_array_add(root, child_node);
}


static void json_add_array2(I8 *json_name, I32 para, I8 *json_name1, I32 para1, JSON  root)
{
     JSON child_node;
     child_node = json_object_new_object();
  
     json_add_int(json_name, para, child_node);
    
     json_add_int(json_name1, para1, child_node);

     json_object_array_add(root, child_node);
}


static void json_add_array3(I8 *json_name, I32 para, I8 *json_name1, I32 para1, I8 *json_name2, I32 para2, JSON root)
{
     JSON child_node;
     child_node = json_object_new_object();
  
     json_add_int(json_name, para, child_node);
    
     json_add_int(json_name1, para1, child_node);
    
     json_add_int(json_name2, para2, child_node);

     json_object_array_add(root, child_node);
}
/*
static void json_add_array5(I8 *json_name, I32 para, I8 *json_name1, I32 para1, I8 *json_name2, I32 para2, I8 *json_name3, I32 para3, I8 *json_name4, I32 para4, JSON root)
{
    
     JSON child_node;
     child_node = json_object_new_object();
  
     json_add_int(json_name, para, child_node);
    
     json_add_int(json_name1, para1, child_node);
    
     json_add_int(json_name2, para2, child_node);

     json_add_int(json_name3, para3, child_node);

     json_add_int(json_name4, para4, child_node);

     json_object_array_add(root, child_node);


}
*/

static I32 json_get_array(char *buf,char *data, int id )
{
    I8 num[DEV_INFO_NUM_LEN];
    memset(num, 0 , DEV_INFO_NUM_LEN);
    JSON sub_elemt, total;
    total = json_tokener_parse(buf);
    enum json_type jsontype;
    if (total == NULL)
    {
        return IF_APP_ERROR;
    } 	
    jsontype = json_object_get_type(total);

    if (jsontype != json_type_array)
        return IF_APP_ERROR;
    sub_elemt = json_object_array_get_idx(total, id);
  
    if (sub_elemt == NULL)
        return IF_APP_ERROR;
     sprintf(data, "%s", json_object_to_json_string(sub_elemt));

     return IF_APP_OK;
}

static  unsigned char caculateCdForImei(unsigned char imei[14])
{
	int i;
	unsigned char sumDigit = 0;
	unsigned char digit;

	for (i = 0; i<7; i++)
	{
		digit = imei[2 * i];
		sumDigit = sumDigit + digit;
		digit = imei[2 * i + 1];
		digit = digit * 2;
		sumDigit = sumDigit + digit / 10 + digit % 10;
	}

	return (10 - sumDigit % 10) % 10;
}

static unsigned char caculateCdForImeiStr(I8 * imei_str)
{
    unsigned char imei[14] = { 0 };
    int i = 0;
	for (; i < 14; ++i)
	{
		imei[i] = imei_str[i] - '0';
	}

    return caculateCdForImei(imei);
}

extern short int UPDATE_GSM_RSSI;
extern short int UPDATE_CDMA_RSSI;
static void json_add_data(I8 * imei, I8 * imsi, I8 * tmsi, I32 uptime_t,  I32 slotId, JSON data_root)
{

    imei[14] = '0' + caculateCdForImeiStr(imei);

    JSON root;
    root = json_object_new_object();
    json_add_int(Cell_Number, slotId, root);
    json_add_string(imei_s, imei, root);
    json_add_string(imsi_s, imsi, root);
#if	0
    if(slotId == 8 || slotId == 9)
    {
        json_add_int(Cell_rssi, UPDATE_GSM_RSSI, root);
    }
    else if(slotId == 10)
    {
        json_add_int(Cell_rssi, UPDATE_CDMA_RSSI, root);
    }
    else{
        json_add_int(Cell_rssi, 255, root);
    }
#endif
    json_add_string(tmsi_s, tmsi, root);
    json_add_int(uptime, uptime_t, root);
    json_object_array_add(data_root, root);
}

static void json_add_mac_data(I8 *mac, I32 uptime_t, JSON data_root)
{
    JSON root;
    root = json_object_new_object();
    json_add_string("mac", mac, root);
    json_add_int(uptime, uptime_t, root);
    json_object_array_add(data_root, root);
}

static I32 cmd_set_ntp_server(NTP_SERVER ntpserver)
{
    FILE *fd;
    fd = fopen(ntpServerFile, "r");
    if (fd == NULL)
    {
        cli_app_log(CLI_LOG_ERROR, "can not open file %s \n", ntpServerFile);
        return IF_APP_ERROR;
    }
    struct stat fileStat;
    stat(ntpServerFile, &fileStat);
    cli_app_log(CLI_LOG_INFO, "ntpserverfile size if %d \n", fileStat.st_size);
    char *fbuf = (char *)malloc(fileStat.st_size);
    memset(fbuf, 0, fileStat.st_size);
    char buf[256] = {0};
    while(fgets(buf, 256, fd) != NULL)
    {
        if (strstr(buf, "list server"))
        {
            memset(buf, 0, 256);
            continue;
        }
        if (strstr(buf, "option enabled"))
        {
            cli_app_log(CLI_LOG_INFO, "ntp file end %s \n", buf);
            break;
        }
        strcat(fbuf, buf);
        memset(buf, 0, 256);
    }
    fclose(fd);
    system("cp /etc/config/system  /mnt/usr");
    system("rm /etc/config/system");
    FILE *fdNew;
    fdNew = fopen(ntpServerFile, "w");
    if (fdNew == NULL)
    {
        free(fbuf);
        system("cp /mnt/usr/system /etc/config");
        return IF_APP_ERROR;
    }
    fwrite(fbuf, strlen(fbuf), 1, fdNew);
    char buf1[256] = {0};
    I32 list = 0;
    for (list = 0; list < ntpserver.num; list++)
    {
        sprintf(buf1, "\tlist server '%s'\n", ntpserver.server[list]);
        fwrite(buf1, strlen(buf1), 1, fdNew);
        memset(buf1, 0, sizeof(buf1));
    }
    fwrite(buf, strlen(buf), 1, fdNew);

    fclose(fdNew);
    free(fbuf);
    return IF_APP_OK;
}

static U32 cmd_get_sys_uptime()
{
    FILE *fd;
    U32 uptime0, freetime;
    fd = fopen("/proc/uptime", "rb");
    fscanf(fd, "%d %d", &uptime0, &freetime);
    fclose(fd);
	
    return uptime0;
}

static I32 cmd_get_num_par(I8 * buf, I8 * string, I8 * dst)
{
   	const I8 *tmp;
    I32 i;
    JSON num_par1;
    num_par1 = json_tokener_parse(buf);
    if (num_par1 == NULL) {
        cli_app_log(CLI_LOG_WARNING, "can not parse data\n");
        return IF_APP_PARSE_ERROR;
    }
    tmp = json_object_get_string(json_object_object_get(num_par1, string));
    if (tmp == NULL) {
        json_object_put(num_par1);	
        return IF_APP_ERROR;
    }
	
    i = strlen(tmp);
    memcpy(dst, tmp, i);
    dst[i] = '\0';
    if(strcmp(dst, num_true) == 0) {
        bzero(dst, strlen(num_true));
        dst[0] = '1';
    } else if (strcmp(dst, num_false) == 0) {
        bzero(dst, strlen(num_false));
        dst[0] = '0';
    }	
    json_object_put(num_par1);
    return i;
}

static I32 cmd_get_char_par(I8 * buf, I8 * string, I8 * dst)
{
    const I8 *tmp;
    I32 i;
    JSON char_par1;
    char_par1 = json_tokener_parse(buf);
    if (char_par1 == NULL) {
        cli_app_log(CLI_LOG_WARNING, "can not parse data\n");
        return IF_APP_PARSE_ERROR;
    }
    tmp = json_object_get_string(json_object_object_get(char_par1, string));
    if(tmp == NULL) {	
        json_object_put(char_par1);
        return IF_APP_ERROR;		
    }
    i = strlen(tmp);

    if (i > DEV_INFO_ADD_LEN)
        return IF_APP_ERROR;
    memcpy(dst, tmp, i);
    dst[i] = '\0';	
    json_object_put(char_par1);
    char_par1 = NULL;
    return i;

} 

static I32 cmd_convert_par(I8 * buf, I32 * data)
{
    *data = 0;
    if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
        sscanf(buf + 2, "%x", data);
    } else {
        sscanf(buf, "%d", data);
    }

    return 0;
}

static void json_add_sniffer_array(I32 slotId, I32 tac, I32 freq, I32 rssi, I32 pci, I32 priority, JSON array1)
{
	JSON string1;
	string1 = json_object_new_object();
    json_add_int(Cell_Number, slotId, string1);
	json_add_int(Cell_tac, tac, string1);
	json_add_int(Cell_freq, freq, string1);
    json_add_int(Cell_rssi, rssi, string1);
    json_add_int(Cell_pci, pci, string1);
    json_add_int("priority", priority, string1);
	json_object_array_add(array1, string1);
}
static void json_add_lte_rru_pa_info(LTE_INFO  lteInfo, I32 id, JSON root)
{
    JSON singel_root;
    singel_root = json_object_new_object();
    json_add_int(rruNum, (id + 1), singel_root);
    json_add_int(paNum, (id + 1), singel_root);
    if (strcmp(cm, lteInfo.Operator) == 0)
    {
        json_add_int(operators, 0, singel_root);
    }
    else if (strcmp(cu, lteInfo.Operator) == 0)
    {
        json_add_int(operators, 1, singel_root);
    }
    else if (strcmp(ct, lteInfo.Operator) == 0)
    {
        json_add_int(operators, 2, singel_root);
    }
    json_add_string(formatType, lteInfo.desc, singel_root);
    json_add_int(Cell_band, atoi(lteInfo.band), singel_root);
    json_object_array_add(root, singel_root); 

}

static void json_add_gsm_rru_pa_info(GSM_INFO*  info, I32 id, JSON root)
{
    JSON singel_root;
    singel_root = json_object_new_object();
    json_add_int(rruNum, (id + 1), singel_root);
    json_add_int(paNum, (id + 1), singel_root);
    if (strcmp("01", info->mnc) == 0)
    {
        json_add_int(operators, 1, singel_root);
    }
    else
    {
        json_add_int(operators, 0, singel_root);
    }
	
    if (strcmp("1800", info->band) == 0)
    {
		json_add_int(Cell_band, atoi(info->band), singel_root);
		json_add_string(formatType, "DCS", singel_root);		
    }
	else
	{
        json_add_string(formatType, "GSM", singel_root);
		json_add_int(Cell_band, atoi(info->band), singel_root);	
	}
	
    json_object_array_add(root, singel_root);
}

static void json_add_cdma_rru_pa_info(CDMA_INFO*  info, I32 id, JSON root)
{
    JSON singel_root;
    singel_root = json_object_new_object();
    json_add_int(rruNum, (id + 1), singel_root);
    json_add_int(paNum, (id + 1), singel_root);
    json_add_int(operators, 2, singel_root);
	json_add_int(Cell_band, 0, singel_root);
	json_add_string(formatType, "CDMA", singel_root);
	
    json_object_array_add(root, singel_root); 
}

static void json_add_wcdma_rru_pa_info(WCDMA_INFO*  info, I32 id, JSON root)
{
    JSON singel_root;
    singel_root = json_object_new_object();
    json_add_int(rruNum, (id + 1), singel_root);
    json_add_int(paNum, (id + 1), singel_root);
    json_add_int(operators, 1, singel_root);
	json_add_int(Cell_band, 1, singel_root);
	json_add_string(formatType, "WCDMA", singel_root);
	
    json_object_array_add(root, singel_root); 
}

static void json_add_tdscdma_rru_pa_info(TDSCDMA_INFO*  info, I32 id, JSON root)
{
    JSON singel_root;
    singel_root = json_object_new_object();
    json_add_int(rruNum, (id + 1), singel_root);
    json_add_int(paNum, (id + 1), singel_root);
    json_add_int(operators, 1, singel_root);
	json_add_int(Cell_band, 33, singel_root);
	json_add_string(formatType, "TDSCDMA", singel_root);
	
    json_object_array_add(root, singel_root); 
}

static void json_add_wifi_rru_pa_info(WIRELESS_WIFI_INFO*  info, I32 id, JSON root)
{
    JSON singel_root;
    singel_root = json_object_new_object();
    json_add_int(rruNum, (id + 1), singel_root);
    json_add_int(paNum, (id + 1), singel_root);
    json_add_int(operators, 4, singel_root);
	json_add_int(Cell_band, 0, singel_root);
	json_add_string(formatType, "WIFI", singel_root);
	
    json_object_array_add(root, singel_root); 
}

I32 cmd_gen_total_bbu_info_string(DEV_CONF * devInfo, I8 *buf)
{
    I32 i = 0, len = 0;
   	time_t rawtime;
 	time(&rawtime);
    JSON info_root, info_array;
    info_root = json_object_new_object();
    info_array = json_object_new_array();
    json_add_int(rlt_string, RST_SUCESS, info_root);
    json_add_string("msg", "", info_root);

    if (devInfo->capLte.isSupported[0] == '1' )
    {
        for (i = LTE_CELL_START; i <= LTE_CELL_END; i++)
        {
            if (devInfo->capLte.lte[i].enable[0] == '1')
                json_add_lte_rru_pa_info(devInfo->capLte.lte[i], i, info_array);
        }
    }

    if (devInfo->capGsm.isSupported[0] == '1')
    {
        json_add_gsm_rru_pa_info(&devInfo->capGsm.gsm1, GSM_CELL_START, info_array);
		json_add_gsm_rru_pa_info(&devInfo->capGsm.gsm2, GSM_CELL_END, info_array);
    }

	if (devInfo->capCdma.isSupported[0] == '1')
	{ 	
	    json_add_cdma_rru_pa_info(&devInfo->capCdma.cdma, GSM_CELL_END + 1, info_array);
	}

	if (devInfo->capWcdma.isSupported[0] == '1')
	{ 	
	    json_add_wcdma_rru_pa_info(&devInfo->capWcdma.wcdma, WCDMA_CELL_START, info_array);
	}

    if (devInfo->capTdscdma.isSupported[0] == '1')
    {
	    json_add_tdscdma_rru_pa_info(&devInfo->capTdscdma.tdscdma, WCDMA_CELL_END, info_array);    
    }

    if (devInfo->capWifi.isSupported[0] == '1')
    {
	    json_add_wifi_rru_pa_info(&devInfo->capWifi, WCDMA_CELL_END + 1, info_array);    
    }

    json_object_object_add(info_root, data_string, info_array);
    json_add_int(uptime, rawtime, info_root);
    len = sprintf(buf, "%s", json_object_to_json_string(info_root));
    json_object_put(info_root);
    return len;
}

I32 cmd_gen_bbu_info_string(DEV_CONF * devInfo, DEV_STATUS * status, I8 *buf)
{
    I32 i = 0, len = 0;
   	time_t rawtime;
 	time(&rawtime);
    JSON info_root, info_array;
    info_root = json_object_new_object();
    info_array = json_object_new_array();
    json_add_int(rlt_string, RST_SUCESS, info_root);
    json_add_string("msg", "", info_root);
    if (devInfo->capLte.isSupported[0] == '1' )
    {
        for (i = LTE_CELL_START; i < LTE_CELL_END; i++)
        {
             json_add_lte_rru_pa_info(devInfo->capLte.lte[i], i, info_array);
        }
    }

    json_object_object_add(info_root, data_string, info_array);
    json_add_int(uptime, rawtime, info_root);
    len = sprintf(buf, "%s", json_object_to_json_string(info_root));
    json_object_put(info_root);
    return len;
}



I32 count_every_bbu_upload_num(I8 *buf, I32 upload_num, I32 cellNum, I32 serialNum)
{
    JSON root;
    int ret = 0;
    if (buf == NULL)
       return IF_APP_ERROR;   
    root = json_object_new_object();
    time_t rawtime;
    time(&rawtime);
    json_add_int(Cell_Number, cellNum, root);
    json_add_int(updatacount, upload_num, root);
    json_add_int(serial_num , serialNum, root);
    json_add_int(uptime, rawtime, root);
    ret = sprintf(buf, "%s", json_object_to_json_string(root));
    json_object_put(root);
    return ret;
}




I32 cmd_gen_lte_devstatus_string(DEV_STATUS * status, I8 * buf, DEV_CONF *conf)
{
    I32 tmp , i = 0, j = 0;
    JSON  PaTemp, PaSwitch, SyncInfo, Pawr, paPower, cellWireless, cellActive, cellInfo; //, bbuStatus;
    PaTemp = json_object_new_array();
    PaSwitch = json_object_new_array();
    Pawr = json_object_new_array();
    paPower = json_object_new_array();
    SyncInfo = json_object_new_array();
    cellWireless = json_object_new_array();
    cellActive = json_object_new_array();
    cellInfo = json_object_new_array();

   //bbuStatus = json_object_new_array();
    bool pa6IsAdd = false, pa7IsAdd = false;
    if (atoi(conf->capLte.isSupported) == 1)
    {

        for (i = 0; i <7; i++)
        {
            if (atoi(conf->capLte.lte[i].enable) == 1)
            {
                if (strcmp(dev_conf.dev.protocol, LOCATION) != 0)
                {
                    if (conf->pa[i].enable[0] == '1' && conf->dev.RFSwitch[0] == '1')
                    {
                        json_add_array2(Cell_Number, (i + 1), "RFSwitch", 1, cellWireless);
                    }
                    else
                    {

                        json_add_array2(Cell_Number, (i + 1), "RFSwitch", 0, cellWireless);
                    }
                }
                else
                {
                    if (conf->pa[i].enable[0] == '1' &&  status->realTimeStatus[i].isActive == 2 )
                    {
                        if (status->tddLocationEnable == 1 && i < 5)
                        {
                            json_add_array2(Cell_Number, (i + 1), "RFSwitch", 1, cellWireless);
                        }
                        else if (status->fddLocationEnable == 1 && i > 4)
                        {
                            json_add_array2(Cell_Number, (i + 1), "RFSwitch", 1, cellWireless);
                        }
                        else
                        {
                            json_add_array2(Cell_Number, (i + 1), "RFSwitch", 0, cellWireless);
                        }
                    }
                    else
                    {

                        json_add_array2(Cell_Number, (i + 1), "RFSwitch", 0, cellWireless);
                    }


                }
                j = i;
                json_add_array2(Cell_Number, (i + 1), "active", status->realTimeStatus[i].isActive, cellActive);
                json_add_array3(Cell_Number, (i + 1), "freq", status->realTimeStatus[i].freq, "tac", atoi(conf->capLte.lte[i].tac), cellInfo);
                json_add_array3(Cell_Number, (i + 1), syncMode, status->bbuStatus[i].syncType, syncStatus, status->bbuStatus[i].syncState, SyncInfo);
                if (strcmp(dev_conf.dev.protocol, LOCATION) != 0)
                {
                    if (atoi(conf->capLte.lte[i].band) == 38)
                    {
                        j = 0;
                    }
                    else if (atoi(conf->capLte.lte[i].band) == 39)
                    {
                        j = 1;
                    }
                    else if (atoi(conf->capLte.lte[i].band) == 40)
                    {
                        j = 2;
                    }
                    else if (atoi(conf->capLte.lte[i].band) == 1)
                    {
                        j = 5;
                        if (pa6IsAdd)
                        {
                            continue;
                        }
                        else
                        {
                            pa6IsAdd = true;
                        }

                    }
                    else if (atoi(conf->capLte.lte[i].band) == 3)
                    {
                        j = 6;
                        if (pa7IsAdd)
                        {
                            continue;
                        }
                        else
                        {
                            pa7IsAdd = true;
                        }
                    }
                }
                json_add_array2(pa_num, (j + 1), temp, status->pa_status[j].temp, PaTemp);
                json_add_array2(pa_num, (j + 1),  pa_s, status->pa_status[j].state, PaSwitch);
                json_add_array2(pa_num , (j + 1), pawr, status->pa_status[j].sw10ratio, Pawr);
                json_add_array2(pa_num, (j + 1), "power", status->pa_status[j].dn_10_power, paPower);
#if 0        
                json_add_array5("cellNumber", (i + 1), "boardtemp", bbu_status[i].boardtemp, "memory", bbu_status[i].memusage, "cputemp", bbu_status[i].cputemp, "cpuusage", bbu_status[i].cpuusage, bbuStatus);
#endif
            }
        }
    }
    
    if (atoi(conf->capGsm.isSupported) == 1)
    {
        json_add_array2(pa_num, 8, temp, status->pa_status[7].temp, PaTemp);
        json_add_array2(pa_num, 8,  pa_s, status->pa_status[7].state, PaSwitch);
        json_add_array2(pa_num , 8, pawr, status->pa_status[7].sw10ratio, Pawr);
        json_add_array2(pa_num, 8, "power", status->pa_status[7].dn_10_power, paPower);
        if (conf->capGsm.gsm1.enable[0] == '1' && conf->dev.RFSwitch[0] == '1')
        {
            json_add_array2(Cell_Number, 8, "RFSwitch", 1, cellWireless);
        }
        else
        {
            json_add_array2(Cell_Number, 8, "RFSwitch", 0, cellWireless);
        }
        if (conf->capGsm.gsm2.enable[0] == '1' && conf->dev.RFSwitch[0] == '1')
        {
            json_add_array2(Cell_Number, 9, "RFSwitch", 1, cellWireless);
        }
        else
        {
            json_add_array2(Cell_Number, 9, "RFSwitch", 0, cellWireless);
        }
        json_add_array2(Cell_Number, 8, "active", status->realTimeStatus[7].isActive, cellActive);
        json_add_array2(Cell_Number, 9, "active", status->realTimeStatus[8].isActive, cellActive);
        json_add_array3(Cell_Number, 8, "arfcn", status->realTimeStatus[7].freq, "lac", status->realTimeStatus[7].lac, cellInfo);
        json_add_array3(Cell_Number, 9, "arfcn", status->realTimeStatus[8].freq, "lac", status->realTimeStatus[8].lac, cellInfo);

        

    }
    if (atoi(conf->capCdma.isSupported) == 1)
    {
        json_add_array2(pa_num, 9, temp, status->pa_status[8].temp, PaTemp);
        json_add_array2(pa_num, 9,  pa_s, status->pa_status[8].state, PaSwitch);
        json_add_array2(pa_num , 9, pawr, status->pa_status[8].sw10ratio, Pawr);
        json_add_array2(pa_num, 9, "power", status->pa_status[8].dn_10_power, paPower);
        if (conf->capCdma.enable[0] == '1' && conf->dev.RFSwitch[0] == '1')
        {
            json_add_array2(Cell_Number, 10, "RFSwitch", 1, cellWireless);
        }
        else
        {
            json_add_array2(Cell_Number, 10, "RFSwitch", 0, cellWireless);
        }
        json_add_array2(Cell_Number, 10, "active", status->realTimeStatus[9].isActive, cellActive);

    }
    if (atoi(conf->capWcdma.isSupported) == 1)
    {
        if (!pa6IsAdd)
        {
            json_add_array2(pa_num, 6, temp, status->pa_status[5].temp, PaTemp);
            json_add_array2(pa_num, 6,  pa_s, status->pa_status[5].state, PaSwitch);
            json_add_array2(pa_num , 6, pawr, status->pa_status[5].sw10ratio, Pawr);
            json_add_array2(pa_num, 6, "power", status->pa_status[5].dn_10_power, paPower);
        }
        if (conf->capWcdma.enable[0] == '1' && conf->dev.RFSwitch[0] == '1')
        {
            json_add_array2(Cell_Number, 11, "RFSwitch", 1, cellWireless);
        }
        else
        {
            json_add_array2(Cell_Number, 11, "RFSwitch", 0, cellWireless);
        }
        json_add_array2(Cell_Number, 11, "active", status->realTimeStatus[10].isActive, cellActive);

    }
    if (atoi(conf->capTdscdma.isSupported) == 1)
    {
        json_add_array2(pa_num, 11, temp, status->pa_status[10].temp, PaTemp);
        json_add_array2(pa_num, 11,  pa_s, status->pa_status[10].state, PaSwitch);
        json_add_array2(pa_num , 11, pawr, status->pa_status[10].sw10ratio, Pawr);
        json_add_array2(pa_num, 11, "power", status->pa_status[10].dn_10_power, paPower);
        if (conf->capTdscdma.enable[0] == '1' && conf->dev.RFSwitch[0] == '1')
        {
            json_add_array2(Cell_Number, 12, "RFSwitch", 1, cellWireless);
        }
        else
        {
            json_add_array2(Cell_Number, 12, "RFSwitch", 0, cellWireless);
        }
        json_add_array2(Cell_Number, 12, "active", status->realTimeStatus[11].isActive, cellActive);

    }

    JSON dev1_st;
    dev1_st = json_object_new_object();
    json_object_object_add(dev1_st, patemp, PaTemp);
    json_add_int(devtemp, status->dev_temp, dev1_st);
    json_add_int(boardtemp, status->board_tmp, dev1_st);
    json_add_int(wireless_s, atoi(conf->dev.RFSwitch), dev1_st);
    json_add_int(sms_s,  atoi(conf->sms.enable), dev1_st);
    json_object_object_add(dev1_st, syncInfo, SyncInfo);
    //json_object_object_add(dev1_st, "bbuStatus", bbuStatus);
    json_object_object_add(dev1_st, PaPower, paPower);
    json_object_object_add(dev1_st, pawr, Pawr);
    json_object_object_add(dev1_st, pa_s, PaSwitch);
    if (strcmp(dev_conf.dev.protocol, CAIPIN) == 0)
    {
        json_object_put(cellWireless);
        json_object_put(cellActive);
        json_object_put(cellInfo);
    }
    else
    {
        json_object_object_add(dev1_st, "BbuRFState", cellWireless);
        json_object_object_add(dev1_st, "BbuActiveState", cellActive);
        json_object_object_add(dev1_st, "BbuInfo", cellInfo);
    }
    if (strcmp(conf->dev.protocol, LOCATION) == 0)
    {
        json_add_int("tddLocationEnable", status->tddLocationEnable, dev1_st);
        json_add_int("fddLocationEnable", status->fddLocationEnable, dev1_st);
    
    }

    json_add_int(sniffer_s, atoi(conf->snfLte.enable), dev1_st);
    json_add_int(cpuload, status->cpu_load, dev1_st);
    json_add_int(memload, status->mem_load, dev1_st);
    if (strcmp(dev_conf.dev.protocol, CAIPIN) != 0)
        json_add_int("disk", status->disk_usage, dev1_st);
    json_add_int(uptime,  cmd_get_sys_uptime(), dev1_st);
    tmp = sprintf(buf, "%s", json_object_to_json_string(dev1_st));

    json_object_put(dev1_st);
    return tmp;

}


I32 cmd_gen_normal_devstatus_string(DEV_STATUS * status, I8 * buf, DEV_CONF *conf)
{
    I32 tmp ;
    JSON dev1_st;
    dev1_st = json_object_new_object();
    json_add_int(patemp, status->pa_status[7].temp, dev1_st);
    json_add_int(devtemp, status->dev_temp, dev1_st);
    json_add_int(boardtemp, status->board_tmp, dev1_st);
    json_add_int(wireless_s, atoi(conf->capGsm.enable), dev1_st);
    json_add_int(pa_s, atoi(conf->pa[7].enable), dev1_st);
    json_add_int(sms_s,  atoi(conf->sms.enable), dev1_st);
    json_add_int(sniffer_s, atoi(conf->snfGsm.enable), dev1_st);
    json_add_int(cpuload, status->cpu_load, dev1_st);
    json_add_int(memload, status->mem_load, dev1_st);
    json_add_int(uptime,  cmd_get_sys_uptime(), dev1_st);
    tmp = sprintf(buf, "%s", json_object_to_json_string(dev1_st));
    json_object_put(dev1_st);
    return tmp;

}

I32 cmd_gen_devstatus_string(I32 devType ,DEV_STATUS * status, I8 * buf, DEV_CONF *conf)
{
    I32 len = 0;
 

    len = cmd_gen_lte_devstatus_string(status, buf, conf);

    return len;
}


I32 cmd_handle_upload_cfg_result(I8 * buf)
{
    if ((strstr(buf, "cfgcode") != NULL) && (strstr(buf, "upresult") != NULL)) {
        return IF_APP_OK;
    }
	
    return IF_APP_ERROR;
}


I32 cmd_handle_statistics_intvl(I8 *buf, I32 *numRcv)
{
    I32 ret ;
    I8 boolnum[DEV_INFO_NUM_LEN];
    I8 num[DEV_INFO_NUM_LEN];
    memset(num, 0 , DEV_INFO_NUM_LEN);
    memset(boolnum, 0 , DEV_INFO_NUM_LEN);
    ret = cmd_get_num_par(buf, "upResult", boolnum);
    
    if (ret == IF_APP_ERROR)
    {
        return ret;
    } 
    else if (atoi(boolnum) == 0)
    {
     
        return IF_APP_OK;
    }
    else
    {
        cmd_get_num_par(buf, "cellNumber", num);
        numRcv[atoi(num) - 1] = 0;
        return IF_APP_INFO_CHNG;

    }    
}


I32 cmd_gen_devinfo_string(DEV_INFO * info, U16 reconnectCode, I8 * buf)
{
    I32 len;
    time_t rawtime;
    JSON root,root1;
    time(&rawtime);
    len = 0;
    root = json_object_new_object();
    char *dev1[] = {devNumber, devName, typeModel, phoneNumber, devPos,height, longitude,latitude, devAddresss, "mac", timestamp,devType, devconf};
    char *dev1_info[] = {info->devNum, info->devName, info->typemodel, info->phoneNum, info->pos_he, info->pos_lo, info->pos_la, info->devAddr, dev_mac};
    int i = 0;
    for (i = 0; i < 4; i++) {
       json_add_string(dev1[i], dev1_info[i], root);

	}	
    root1 = json_object_new_object();
    for (i = 5; i < 8; i++) {
        json_add_double(dev1[i], atof(dev1_info[i-1]), root1);
    }
    json_object_object_add(root, dev1[4], root1);
    for (i = 8; i < 10; i++) {
        json_add_string(dev1[i], dev1_info[i-1], root);
    }
    json_add_int(paMaxPower, 0, root);
    json_add_int(dev1[11], atoi(info->devtype), root);
    json_add_int(dev1[12], 0x0001, root);
    if (strcmp(dev_conf.dev.protocol, CAIPIN) != 0)
    {
        json_add_int("reconnectCause", reconnectCode, root);
        json_add_string("siteArea", info->siteArea, root);
    }
    json_add_int(dev1[10], (U32)rawtime, root);
    len = sprintf(buf, "%s", json_object_to_json_string(root));
    json_object_put(root);

    return len;
}




I32 cmd_gen_gpsstatus_string(GPSINFO * gpsInfo, I8 * buf)
{
    I32 tmp;
    time_t rawtime;
    time(&rawtime);
    JSON gpsstatus ,gps_info;
    gpsstatus = json_object_new_object();
    gps_info = json_object_new_object();
    json_add_int(rlt_string,  RST_SUCESS, gpsstatus);
    json_add_string(msg_string, "", gpsstatus);
    json_add_double(longitude, gpsInfo->longitude, gps_info);
    json_add_double(latitude, gpsInfo->latitude, gps_info);
    json_add_double(altitude, gpsInfo->altitude, gps_info);
    json_object_object_add(gpsstatus, GpsInfo, gps_info);
    json_add_int(timestamp, rawtime, gpsstatus);
    tmp = sprintf(buf, "%s", json_object_to_json_string(gpsstatus));
    cli_app_log(CLI_LOG_INFO, "gps msg is %s\n", buf);
    json_object_put(gpsstatus);
    return tmp;
}

I32	cmd_gen_gpsboardstatus_string(GPSBOARDINFO * gpsInfo, I8 * buf)
{
	I32 tmp;
    time_t rawtime;
    time(&rawtime);
    JSON gpsstatus ,gps_info;
    gpsstatus = json_object_new_object();
    gps_info = json_object_new_object();

    json_add_double(longitude, gpsInfo->longitude, gps_info);
    json_add_double(latitude, gpsInfo->latitude, gps_info);
    json_add_int("starcnt", gpsInfo->starcnt, gps_info);
    json_object_object_add(gpsstatus, GpsInfo, gps_info);
    json_add_int(timestamp, rawtime, gpsstatus);
    tmp = sprintf(buf, "%s", json_object_to_json_string(gpsstatus));
    cli_app_log(CLI_LOG_INFO, "gps msg is %s\n", buf);
    json_object_put(gpsstatus);
    return tmp;

}

I32 cmd_gen_sys_info_string(SYS_INFO *sysInfo, I8 *buf)
{
    I32 tmp = 0;
    struct tm tm = {0};
    I8 day[32];
    memset(day, 0, 32);
    time_t rawtime , updatetime;
    time(&rawtime);
    JSON sys_info, version_info;
    sys_info = json_object_new_object();
    version_info = json_object_new_object();
    json_add_int(rlt_string, RST_SUCESS, sys_info);
    json_add_string(msg_string, "", sys_info);
    json_add_string("hardWareVer", "0001", version_info);
    json_add_string("osVer", sysInfo->os_version, version_info);
    json_add_string("currentSoftVer", sysInfo->sw_version, version_info);
    json_add_string("originalSoftVer", sysInfo->sw_originalVer, version_info);
    sprintf(day, "%s %s", sysInfo->build_date, sysInfo->build_time);
    strptime(day, "%b %d %Y%H:%M:%S", &tm); 
    updatetime = mktime(&tm);
    json_add_int("devFactoryTime", updatetime, version_info);
    json_add_int("softUpdateTime", updatetime, version_info);
    json_add_int("currVerNum", atoi(sysInfo->currVernum), version_info);
    json_object_object_add(sys_info, "VersionInfo", version_info);
    json_add_int(timestamp, rawtime, sys_info);
    tmp = sprintf(buf, "%s", json_object_to_json_string(sys_info));
    json_object_put(sys_info);
    return tmp;
}



I32 cmd_check_msg_num(I8 *buf, I32 buf_len)
{
    I32 len = 0, i = 0 , j = 0;
    while(len < buf_len)
    {
       if (buf[len] == '{')
           i++;
       else if (buf[len] == '}')
       {
           j++;
           if (j == i)
             break; 
       }
       len++;
    }
    return ++len;
}

I32 cmd_handle_mng_heartbeat_string(I8 * buf)
{
    I32 code, len;
    I8 num[DEV_INFO_NUM_LEN];
    bzero(num, DEV_INFO_NUM_LEN);

    len = cmd_get_num_par(buf, stat_string, num);
    if (len == IF_APP_PARSE_ERROR)
        return IF_APP_PARSE_ERROR;
    if (len != IF_APP_ERROR) {
        cmd_convert_par(num, &code);
        if (code != HT_SRV_STATUS_OK) {
            bzero(num, DEV_INFO_NUM_LEN);
            cmd_get_num_par(buf, code_string, num);
            cmd_convert_par(num, &code);
        }

        return code;
    }
    return IF_APP_ERROR;
}

I32 cmd_handle_upload_result(I8 * buf)
{
    if (strstr(buf, "upresult") != NULL) {
        return IF_APP_OK;
    }
	
    return IF_APP_ERROR;
}

I32 cmd_get_upload_srctime(I8 *buf)
{
    I8 num[DEV_INFO_NUM_LEN];
    bzero(num, DEV_INFO_NUM_LEN);
    if ( cmd_get_num_par(buf, "srcTime", num) )
    {
        return atoi(num);
    }
    return IF_APP_ERROR;
}



I32 cmd_gen_mng_heartbeat_string(I8 * buf, I8 *devNum, I32 addDevNum, U32 buf_len, U32 state, I8 * msg)
{
  	I32 ret = IF_APP_ERROR;
  	time_t rawtime;
    time(&rawtime);

	JSON cmd_gen_mng_str;
	cmd_gen_mng_str = json_object_new_object();
	if (state == HT_CL_STATUS_OK || state == HT_CL_STATUS_START || state == HT_CL_STATUS_DOING) {
        if (addDevNum == 1)
            json_add_string("deviceNumber", devNum, cmd_gen_mng_str);
		json_add_int(stat_string, state, cmd_gen_mng_str);
		json_add_int(timestamp, rawtime, cmd_gen_mng_str);
		ret = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_mng_str));
		json_object_put(cmd_gen_mng_str);
		return ret;
	}	
	if (state > HT_CL_EX_OTHER || state < HT_CL_EX_SMS) {
		json_object_put(cmd_gen_mng_str);
		return IF_APP_ERROR;
	}
	if (msg == NULL) {
        if (addDevNum == 1)
            json_add_string("deviceNumber", devNum, cmd_gen_mng_str);
		json_add_int(stat_string, HT_CL_STATUS_EX,cmd_gen_mng_str);
		json_add_int("code", state, cmd_gen_mng_str);
		json_add_int(timestamp, rawtime, cmd_gen_mng_str);
		ret  = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_mng_str));
	}
	else {
         if (addDevNum == 1)
            json_add_string("deviceNumber", devNum, cmd_gen_mng_str);
		json_add_int(stat_string, HT_CL_STATUS_EX, cmd_gen_mng_str);
		json_add_int("code", state,cmd_gen_mng_str);
		json_add_string("msg", msg,cmd_gen_mng_str);
		json_add_int(timestamp, rawtime,cmd_gen_mng_str);
		ret = sprintf(buf,"%s", json_object_to_json_string(cmd_gen_mng_str));
	}
	
	json_object_put(cmd_gen_mng_str);
	buf[ret] = 0;
	return ret;

}

I32 cmd_gen_mng_response(I8 * buf, U32 buf_len, U32 state, I8 * reason, I32 code , I32 type, COMMANDLIST cmdList)
{
  	I32 ret = IF_APP_ERROR;
	time_t rawtime;
 	time(&rawtime);
   	if (buf_len < CMD_LENGTH_MIN) {
     	  	 return IF_APP_ERROR;
  	  }

	JSON cmd_gen_mng_res;
    
	cmd_gen_mng_res = json_object_new_object();
	if (state == RST_SUCESS || state == RST_DOING) {
		json_add_int("result", state, cmd_gen_mng_res);
        json_add_int("cmdType", type, cmd_gen_mng_res);

        json_add_int("code", code, cmd_gen_mng_res);
		json_add_string("msg", (reason == NULL ? "": reason ), cmd_gen_mng_res);
		json_add_int(timestamp, rawtime, cmd_gen_mng_res);

		ret = sprintf(buf,"%s", json_object_to_json_string(cmd_gen_mng_res));
	}
	else if	(state == RST_FAILED) {
		json_add_int("result", state, cmd_gen_mng_res);
        json_add_int("cmdType", type, cmd_gen_mng_res);
        json_add_int("code", code, cmd_gen_mng_res);
		json_add_string("msg", (reason == NULL ? "": reason ), cmd_gen_mng_res);
		json_add_int(timestamp, rawtime, cmd_gen_mng_res);
		ret = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_mng_res));
	}
	else {
		json_object_put(cmd_gen_mng_res);
		return IF_APP_ERROR;
	}
	buf[ret] = 0;
	json_object_put(cmd_gen_mng_res);
	return ret ;


}

static I32 cmd_get_mng_hdr(I8 * buf, I32 * code)
{
	I32 type = IF_APP_ERROR;
	I8 num[DEV_INFO_NUM_LEN];
	I32 len;
	bzero(num, DEV_INFO_NUM_LEN);
	len = cmd_get_num_par(buf, type_string, num);                               
	if (len != IF_APP_ERROR) {
		cmd_convert_par(num, &type);
		bzero(num, DEV_INFO_NUM_LEN);
		len = cmd_get_num_par(buf, code_string, num);
		cmd_convert_par(num, code);
		
        cli_app_log(CLI_LOG_NOTICE, 
					"type is %d %x, code is %d,%x\n", type,
                    type, *code, *code);
    }

    return type;
}
static I32 cmd_handle_ctrl_msg(I32 devType, I32 code, I8 * cfg, I8 * data)
{
    I32 ret = IF_APP_ERROR;
    I8 num[DEV_INFO_ADD_LEN];
	bzero(num, DEV_INFO_ADD_LEN);
    I8 data1[DEV_INFO_ADD_LEN];
    memset(data1, 0, DEV_INFO_ADD_LEN);
    I8 data2[DEV_INFO_ADD_LEN];
    memset(data2, 0, DEV_INFO_ADD_LEN);
    DEV_INFO *devInfo = (DEV_INFO *)data;
    SMS_INFO *smsInfo = (SMS_INFO *)data;
    SRV_INFO *srvInfo = (SRV_INFO *)data;
    SNF_INFO *snfInfo = (SNF_INFO *)data;
    PA_INFO *paInfo = (PA_INFO *)data;
    FTP_INFO *ftpInfo = (FTP_INFO *)data;
	MSSAGE_INFO * msg;
    I32 len = 0;

    I32 len1 , id = 0;
    switch (code) {
        case CT_RESRART:
            len = cmd_get_num_par(cfg, delay_string, num);
            if (len != IF_APP_ERROR) {
                cmd_convert_par(num, (I32 *) data);
                *(I32 *) data /= 1000;  //converto to s
            } else {
                *(I32 *) data = 1;
            }
            cli_app_log(CLI_LOG_CRITICAL, "system reboot after %d s \n", *(I32 *)data);
            ret = IF_APP_REBOOT;
            break;

        case CT_SLEEP:
            cli_app_log(CLI_LOG_CRITICAL, "system sleep \n");
            ret = IF_APP_SLEEP;
            break;

        case CT_WAKEUP:
            cli_app_log(CLI_LOG_CRITICAL, "system wake up \n");
            ret = IF_APP_WAKEUP;
            break;

        case CT_SMS_SEND:
            len = cmd_get_char_par(cfg, data_string, num);
            if (len == IF_APP_ERROR)
            {
                ret = IF_APP_ERROR;
                break;
            }
            msg = (MSSAGE_INFO *)data;
            cmd_get_char_par(num, phoneNumber, msg->phonenum);						
            len = cmd_get_char_par(num, message, msg->message);
            if (len == IF_APP_ERROR)
                ret = IF_APP_ERROR;
            else            
                ret = IF_APP_SMS_SND;
            break;

        case CT_TIME_SYNC:
            len = cmd_get_num_par(cfg, timestamp, num);
            if (len != IF_APP_ERROR) {
                cmd_convert_par(num, (I32 *) data);
                ret = IF_APP_TIME_SYNC;
                cli_app_log(CLI_LOG_INFO, "system time sync\n");
            }            
            break;

        case CT_SMS_OFF:
            smsInfo->enable[0] = '0';
            cli_app_log(CLI_LOG_INFO, "3gdongle sms off \n");
            ret = IF_APP_SMS_SWITCH;
            break;

        case CT_SMS_ON:
            smsInfo->enable[0] = '1';
            cli_app_log(CLI_LOG_INFO, "3gdongle sms on \n");
            ret = IF_APP_SMS_SWITCH;
            break;

        case CT_SMS_MAIN_SWITCH:
            len = cmd_get_char_par(cfg, data_string, num);
            if (len > IF_APP_ERROR)
            {
                 len = cmd_get_char_par(num, "smsMgrSwitch", data1);
                 if (len > IF_APP_ERROR)
                 {
                    if(atoi(data1) == 1)
                       smsInfo->msgEnable[0] = '1';
                    else
                       smsInfo->msgEnable[0] = '0';
                    ret = IF_APP_SMS_MAIN_SWITCH;
                    
                 }
            }   
            break;      


        case CT_SRV_UPDATA_OFF:
            srvInfo->enable[0] = '0';
            cli_app_log(CLI_LOG_NOTICE, "stop upload data to server \n");
            ret = IF_APP_UPDATA_SWITCH;
            break;

        case CT_SRV_UPDATA_ON:
            srvInfo->enable[0] = '1';
            cli_app_log(CLI_LOG_NOTICE, "start upload data to server \n");
            ret = IF_APP_UPDATA_SWITCH;
            break;

        case CT_AIR_SNIFFER_OFF:
            snfInfo->enable[0] = '0';
            cli_app_log(CLI_LOG_INFO, "set gsm auto sniffer off \n");
            ret = IF_APP_SNIFFER_SWITCH;
            break;

        case CT_AIR_SNIFFER_ON:
            snfInfo->enable[0] = '1';
            cli_app_log(CLI_LOG_INFO, "set gsm auto sniffer on \n");
            ret = IF_APP_SNIFFER_SWITCH;
            break;

        case CT_RUNSTATS_OFF:
            srvInfo->enable[0] = '0';
            cli_app_log(CLI_LOG_NOTICE, "set send runstats to server off \n");
            ret = IF_APP_RUNTIME_SWITCH;
            break;

        case CT_RUNSTATS_ON:
            srvInfo->enable[0] = '1';
            cli_app_log(CLI_LOG_NOTICE, "set send runstats to server on \n");
            ret = IF_APP_RUNTIME_SWITCH;
            break;

        case CT_RESETTOFACTORY:
            cli_app_log(CLI_LOG_CRITICAL, "factory reset system \n");
            ret = IF_APP_FACTORY_DEAULT;
            break;

        case CT_CLEAN_DATA:
            cli_app_log(CLI_LOG_CRITICAL, "clean device data \n");
            ret = IF_APP_CLEAN_DATA;
            break;

        case CT_WIRELISS_OFF:
            devInfo->RFSwitch[0] = '0';
            cli_app_log(CLI_LOG_NOTICE, "set device wireless off \n");
            ret = IF_APP_WIFI_SWITCH;
            break;

        case CT_WIRELISS_ON:
            devInfo->RFSwitch[0] = '1';
            cli_app_log(CLI_LOG_NOTICE, "set device wireless on \n");
            ret = IF_APP_WIFI_SWITCH;
            break;

        case CT_BBU_PW_OFF:

            len = cmd_get_char_par(cfg, data_string, num);
            if (len != IF_APP_ERROR)
            {
                len1 = cmd_get_char_par(num, palist, data1);
                memset(num, 0 , DEV_INFO_ADD_LEN);
                if (len1 != IF_APP_ERROR)				
	            {
                    while(json_get_array(data1, data2,  id) == 0)
                    {
                        cmd_get_num_par(data2, Cell_Number, num);
                        paInfo = (PA_INFO *) (data + sizeof(PA_INFO) * (atoi(num) - 1));
                        paInfo->enable[0] = '0';
                        cli_app_log(CLI_LOG_NOTICE, "set  BBU[%d] off \n", atoi(num));
                        memset(data2, 0 , DEV_INFO_ADD_LEN);
                        id++;
                    }
                        
                    ret =  IF_APP_PA_OFF;
                }
            }
         
            break;

        case CT_BBU_PW_ON:

            len = cmd_get_char_par(cfg, data_string, num);
            if (len != IF_APP_ERROR)
            {
                len1 = cmd_get_char_par(num, palist, data1);
                memset(num, 0 , DEV_INFO_ADD_LEN);
                if (len1 != IF_APP_ERROR)				
	            {
                    while(json_get_array(data1, data2,  id) == 0)
                    {
                        cmd_get_num_par(data2, Cell_Number, num);
                        paInfo = (PA_INFO *) (data + sizeof(PA_INFO) * (atoi(num) - 1));
                        paInfo->enable[0] = '1';
                        cli_app_log(CLI_LOG_NOTICE, "set lte BBU[%d] on \n", atoi(num));
                        memset(data2, 0 , DEV_INFO_ADD_LEN);
                        id++;
                    }                        
                    ret =  IF_APP_PA_ON;
                }
            }
            
            break;

        case CT_SNIFFER:

            len = cmd_get_char_par(cfg, data_string, num);
            if (len != IF_APP_ERROR)
            {   
                int j = 0;
                I8 arraydata[DEV_INFO_ADD_LEN];
                memset(arraydata, 0, DEV_INFO_ADD_LEN);    
                while (json_get_array(num, arraydata, j) == 0)
                {
                    int  k = 0;
                    U32 freqlist[12] = {0};
                    char *pp;
                    
                    j++;
                    cmd_get_char_par(arraydata, Cell_Number, data1);
                    if (atoi(data1) < 0 || (atoi(data1) > CELL_NUM))
                    {
                        //cmdList.commandList[cmdList.commandNum].cmdId = REQUEST_GSM_MANUAL_SNIFFER;
                        //ret = IF_APP_GSM_MANUAL_SNIFER;
                        continue;
                    }
                    if (atoi(data1) == 8)
                    {
                        ret = IF_APP_GSM_MANUAL_SNIFER; //only for gsm
                        break;
                    }
                    else if (atoi(data1) == 1)
                    {
                        ret = IF_APP_LTE_MANUAL_SNIFFER;
                        break;
                    }
                    else if (atoi(data1) == 11)
                    {
                        ret = IF_APP_WCDMA_MANUAL_SNIFFER;
                        break;
                    }
                    cmdList.commandList[cmdList.commandNum].cellId = atoi(data1);
                    memset(data1, 0 ,DEV_INFO_ADD_LEN);      
                       
                    if (cmd_get_char_par(arraydata, "snifferband", data1))
                    {
                        cmd_get_char_par(data1, "band", data2);

                        I8 data3[DEV_INFO_ADD_LEN];
                        memset(data3, 0, DEV_INFO_ADD_LEN);

                        I8 data4[DEV_INFO_NUM_LEN];
                        memset(data4, 0, DEV_INFO_NUM_LEN);
                        id = 0;
                        while(json_get_array(data2, data3, id) == 0)
                        {
                            id++;
                            len1 = cmd_get_char_par(data3, "scope", data3);
                            if (len1 == IF_APP_ERROR)
                                 break;
                            pp = strtok(data3, "-");
                            if (pp == NULL)
                                 break;
                            freqlist[k++] = atoi(pp);
                            while((pp = strtok(NULL, "-")) != NULL)
                                freqlist[k++] = atoi(pp);
                            if (k >= 12)
                               break;                      
                            memset(data3, 0, DEV_INFO_ADD_LEN);
                       
                        }
                 
                        memset(data2, 0, DEV_INFO_ADD_LEN);
                        memset(data3, 0, DEV_INFO_ADD_LEN);
                        memset(data4, 0, DEV_INFO_NUM_LEN);
                    
                        cmd_get_char_par(data1, "freq", data2);
                        id = 0;
                        while(json_get_array(data2, data3, id) == 0)
                        {
                            id++;
                            len1 = cmd_get_char_par(data3, "freq", data4);
                            if (len1 == IF_APP_ERROR)
                                break;
                            freqlist[k++] = atoi(data4);
                            freqlist[k++] = atoi(data4);
                            memset(data3, 0, DEV_INFO_ADD_LEN);
                            memset(data4, 0, DEV_INFO_NUM_LEN);

                            if (k >= 12)
                                break;
                        }

                    }
                    cmdList.commandList[cmdList.commandNum].cmdId = REQUEST_EUTRAN_SNIFFER;
                    memcpy(cmdList.commandList[cmdList.commandNum].data, freqlist, sizeof(I32) * 12);
                    cmdList.commandNum++;
                    
                    memset(arraydata, 0, DEV_INFO_ADD_LEN);
                    ret = IF_APP_MANUAL_SNIFER;
                }
               
            }
            else
                ret = IF_APP_GSM_MANUAL_SNIFER;
            break;
       case CT_BBU_SOFT_RESTART:
       case CT_BBU_RESTART:
             len = cmd_get_char_par(cfg, data_string, num);
             if (len > IF_APP_OK)
             {
                 len = cmd_get_char_par(num, Cell_Number, data1);
                 if ((atoi(data1) > 0) && (atoi(data1) <= CELL_NUM))
                 {
                     *(I32 *)data = atoi(data1);
                     cli_app_log(CLI_LOG_NOTICE, "restart bbu[%d] \n", atoi(data1));
                     if (code == CT_BBU_RESTART)
                     	return IF_APP_MANUAL_REBOOT_BBU;
                     else
                     {
                         if (atoi(data1) > 0 && atoi(data1) <= LTE_CELL_NUM)
                             ret = IF_APP_RESTART_LTE_BBU;
                         else if (atoi(data1) == 8 || atoi(data1) == 9)
                             ret = IF_APP_RESTART_GSM_BBU;
                         else if (atoi(data1) == 10)
                             ret = IF_APP_RESTART_CDMA_BBU;
                         else if (atoi(data1) == 11)
                             ret = IF_APP_RESTART_WCDMA_BBU;
                         else if (atoi(data1) == 12)
                             ret = IF_APP_RESTART_TDSCDMA_BBU;
                     }
                 }                
             }
             break; 

        case CT_SET_BLACKLIST_ON  :

             len = cmd_get_char_par(cfg, "imsiList", num);
             cli_app_log(CLI_LOG_INFO, "location data  %s\n", num);
             if (len > IF_APP_OK)
             {   
                 I32 imsi_num = 0;
                 while(json_get_array(num, data1, id) == 0)
                 {
                     cmd_get_char_par(data1, imsi_s, data2);
                     if (strlen(data2) != 15)
                     {
                         id++;
                         continue;
                     }
                     memcpy(data+(imsi_num * 16) , data2, 15);
                     imsi_num++;
                     id++;
                     if (json_get_array(num, data1, id) == 0)
                         data[(imsi_num * 16) - 1] = ';';
                     memset(data1, 0 ,DEV_INFO_ADD_LEN);
                     memset(data2, 0, DEV_INFO_ADD_LEN);
                 }
                 if (imsi_num > 0)
                     ret = IF_APP_SET_BLACKLIST_ON;

                 cli_app_log(CLI_LOG_INFO, "data === %s , imsi_num === %d  %d\n", data + 28, imsi_num, ret );
             }    
             break;   
      
    
        case CT_SET_BLACKLIST_OFF:
             cli_app_log(CLI_LOG_INFO, "set blackList off \n");
             ret = IF_APP_SET_BLACKLIST_OFF;
             break;

        case CT_UE_REDIRECT_ON:
             len = cmd_get_char_par(cfg, data_string, num);
             if (len > IF_APP_OK)
             {
                 UE_REDIRECT ueRedirect= {0, 0, 0, {0}};
                 len1 = cmd_get_char_par(num, Cell_Number, data1);
                 if (len1 > IF_APP_OK )
                    ueRedirect.slotId = atoi(data1);
                 else
                     break;
                 memset(data1, 0, DEV_INFO_ADD_LEN);
                 len1 = cmd_get_char_par(num, "ranType", data1);
                 if (len1 > IF_APP_OK)
                     ueRedirect.ranType = atoi(data1);
                 else
                     break;
                 memset(data1, 0, DEV_INFO_ADD_LEN);
                 len1 = cmd_get_char_par(num, "band", data1); 
                 if (len1 > IF_APP_OK)
                      ueRedirect.band = atoi(data1);
                 else
                     break;
                 memset(data1, 0, DEV_INFO_ADD_LEN);
                 len1 = cmd_get_char_par(num, "freqList", data1);
                 if (len1 > IF_APP_OK)
                 {
                     I8 *freq;
                     I32 i = 0;
                     freq = strtok(data1, ",");
                     if (freq != NULL)
                         ueRedirect.freqList[i++] = atoi(freq);
                     else
                         break;
                     while ((freq = strtok(NULL, ",")) != NULL)
                         ueRedirect.freqList[i++] = atoi(freq);
          
                 }
                 else
                    break;
                 memcpy(data, &ueRedirect, sizeof(UE_REDIRECT));
                 memset(data1, 0, DEV_INFO_ADD_LEN);
                 len1 = cmd_get_char_par(num, "imsiList", data1);
                 if (len1 > IF_APP_OK)
                 {
        
                     I32 imsi_num = 0;
                     I8 data3[DEV_INFO_ADD_LEN];
                     memset(data3, 0, DEV_INFO_ADD_LEN);
                     while(json_get_array(data1, data2, id) == 0)
                     {
                         cmd_get_char_par(data2, imsi_s, data3);
                     
                         if (strlen(data3) != 15)
                         {
                             id++;
                             continue;
                         }
                    
                         memcpy(data+(imsi_num * 16) + sizeof(UE_REDIRECT), data3, 15);
                         imsi_num++;
                         id++;
                         if (json_get_array(data1, data2, id) == 0)
                             data[(imsi_num * 16) + sizeof(UE_REDIRECT) - 1] = ';';
                         memset(data2, 0 ,DEV_INFO_ADD_LEN);
                         memset(data3, 0, DEV_INFO_ADD_LEN);
                     }
                 }
                 cli_app_log(CLI_LOG_INFO, "set ue redirect bbu[%d] band[%d] \n", ueRedirect.slotId, ueRedirect.band);
                 ret = IF_APP_UE_REDIRECT_ON;

             }            
             break;

        case CT_UE_REDIRECT_OFF:
             len = cmd_get_char_par(cfg, data_string, num);
             if (len > IF_APP_OK)
             {
                  UE_REDIRECT ueRedirect= {0, 0, 0, {0}};
                 len1 = cmd_get_num_par(num, Cell_Number, data1);
                 if (len1 > IF_APP_OK)
                      ueRedirect.slotId= atoi(data1);
                 else
                     break;
                 memcpy(data, &ueRedirect, sizeof(UE_REDIRECT));
                 cli_app_log(CLI_LOG_INFO, "set ue redirect off bbu[%d] \n", ueRedirect.slotId);
                 ret = IF_APP_UE_REDIRECT_OFF;
            
             }
             break;

        case CT_PA_POWER_ON:
            len = cmd_get_char_par(cfg, data_string, num);
            if (len != IF_APP_ERROR)
            {
                cmd_get_char_par(num, "paNumber", data1);
                if (atoi(data1) < 0 || atoi(data1) > 12)
                    break;
                *(I32 *)data = atoi(data1);
                ret = IF_APP_PA_POWER_ON;
            }
            break;

        case CT_PA_POWER_OFF:
            len = cmd_get_char_par(cfg, data_string, num);
            if (len != IF_APP_ERROR)
            {
                cmd_get_char_par(num, "paNumber", data1);
                if (atoi(data1) < 0 || atoi(data1) > 12)
                    break;
                *(I32 *)data = atoi(data1);
                ret = IF_APP_PA_POWER_OFF;
            }
            break;

        case CT_FTP_REPORT_ON:
            ftpInfo->ftpenable[0] = '1';
            ret = IF_APP_FTP_REPORT_SWITCH;
            break;

        case CT_FTP_REPORT_OFF:
            ftpInfo->ftpenable[0] = '0';
            ret = IF_APP_FTP_REPORT_SWITCH;
            break;

        case CT_SCANMODE_ON:
             len = cmd_get_char_par(cfg, data_string, num);
             if (len > IF_APP_OK)
             {
                 len = cmd_get_char_par(num, "scanPeriod", data1);
     
                 *(I32 *)data = atoi(data1);
           
                 return IF_APP_BBU_SCAN_ON;
             }
             break;

        case CT_SCANMODE_OFF:
             ret =  IF_APP_BBU_SCAN_OFF;
             break;

	    case CT_REBOOT_DEVICE:
            ret = IF_APP_REBOOT_DEVICE;
            break;
    
        case CT_REMOTE_SSH_ON:
            {
                cli_app_log(CLI_LOG_INFO, "recevice ct remoet ssh on command \n");
                len = cmd_get_char_par(cfg, data_string, num);
                if (len > IF_APP_OK)
                {
                    REMOTE_SSH *remoteSsh = (REMOTE_SSH *)data;
                    if (cmd_get_char_par(num, "port", remoteSsh->port) < IF_APP_OK)
                    {
                        cli_app_log(CLI_LOG_ERROR, "get port remote ssh failed \n");
                        break;
                    }
                    if (cmd_get_char_par(num, "user", remoteSsh->user) < IF_APP_OK)
                    {
                        cli_app_log(CLI_LOG_ERROR, "get user  remote ssh failed \n");
                        break;
                    }
                    if (cmd_get_char_par(num, "password", remoteSsh->password) < IF_APP_OK)
                    {
                        cli_app_log(CLI_LOG_ERROR, "get password  remote ssh failed \n");
                        break;
                    }
                    if (cmd_get_char_par(num, "serverAddr", remoteSsh->serverAddr) < IF_APP_OK)
                    {
                        cli_app_log(CLI_LOG_ERROR, "get serverAddr  remote ssh failed \n");
                        break;
                    }
                    ret = IF_APP_REMOTE_SSH_ON;
                }

            }
            break;

        case CT_REMOTE_SSH_OFF:
            cli_app_log(CLI_LOG_INFO, "recevice ct remote ssh off command \n");
            ret = IF_APP_REMOTE_SSH_OFF;
            break;

		case CT_GSM_SNIFFER_ON:
			snfInfo->enable[0] = '1';
			ret = IF_APP_GSM_SNF_SWITCH;
			break;

		case CT_GSM_SNIFFER_OFF:
			snfInfo->enable[0] = '0';
			ret = IF_APP_GSM_SNF_SWITCH;
			break;
			
		case CT_LOCATION_ON :
			{
				len = cmd_get_char_par(cfg, data_string, num);
				cli_app_log(CLI_LOG_INFO, "location data  %s\n", num);
				if (len > IF_APP_OK)
				{  

					len = cmd_get_char_par(num, "imsiList", data1);
					if (len < IF_APP_OK)
					{
						ret = IF_APP_ERROR;
					}
					strcpy(data, data1);
                    memset(data1, 0, sizeof(data1));
                    len = cmd_get_char_par(num, "DeplexMode", data1);
					if (len > IF_APP_OK)
					{
						if ( strcmp(data1, "TDD") == 0 )
						{
							ret = IF_APP_TDD_LOCATION_ON;
						}
						else
						{
							ret = IF_APP_FDD_LOCATION_ON;
						}
					}
                    memset(data1, 0, sizeof(data1));
                    cmd_get_char_par(num, "userName", data1);
                    cmd_get_char_par(num, "userId", data2);
                    if (strcmp(data1, "root") != 0)
                    {
                        isRoot = false;

                        saveUserRecord(data, data1, data2);
                    }
                    else
                        isRoot = true;
					cli_app_log(CLI_LOG_NOTICE, "imsiList is %s user %s id %s \n", data, data1, data2);
				}    
			}
			break; 

        case CT_LOCATION_OFF:
			 cmd_get_char_par(cfg, data_string, num);
             cmd_get_char_par(num, "DeplexMode", data1);
             if ( strcmp(data1, "TDD") == 0 )
             {
                 ret = IF_APP_TDD_LOCATION_OFF;
             }
             else
             {
                 ret = IF_APP_FDD_LOCATION_OFF;
             }
             break;
			 
        default:
             break;
    }
    return ret;
}

static I32 cmd_handle_conf_msg(I32 devType, I32 code, I8 * buf, I8 *rcvdata)
{
    I8 *tmp1;
    I8 num[DEV_INFO_NUM_LEN];
    bzero(num, DEV_INFO_NUM_LEN);
    I8 data[DEV_INFO_ADD_LEN];
    memset(data, 0, DEV_INFO_ADD_LEN);
    I8 data1[DEV_INFO_ADD_LEN];
    memset(data1, 0, DEV_INFO_ADD_LEN);

    U32 tmp3;
    I32 *tmp2;
    I32 len = 0, id=0;
    DEV_INFO *devInfo = (DEV_INFO *)rcvdata;
    WORKMODE *workMode = (WORKMODE *)rcvdata;
    WIRELESS_CDMA_INFO *wCdma = (WIRELESS_CDMA_INFO *)rcvdata;
    WIRELESS_WCDMA_INFO *wWcdma = (WIRELESS_WCDMA_INFO *)rcvdata;
    WIRELESS_TDSCDMA_INFO *wTdscdma = (WIRELESS_TDSCDMA_INFO *)rcvdata;
    WIRELESS_WIFI_INFO *wWifi = (WIRELESS_WIFI_INFO *)rcvdata;
    WIRELESS_GSM_INFO *wGsm = (WIRELESS_GSM_INFO *)rcvdata;
    WIRELESS_LTE_INFO *wLte = (WIRELESS_LTE_INFO *)rcvdata;
    SMS_INFO *smsInfo = (SMS_INFO *)rcvdata;
    SRV_INFO *srvInfo = (SRV_INFO *)rcvdata;
    SNF_INFO *snfInfo = (SNF_INFO *)rcvdata;
    STORAGE_INFO *storage = (STORAGE_INFO *)rcvdata;
    PA_INFO *paInfo = (PA_INFO *)rcvdata;
    FTP_INFO *ftpInfo = (FTP_INFO *)rcvdata;
    RESTART_INFO *restartInfo = (RESTART_INFO *)rcvdata;
    LTE_INFO *lInfo;
    GSM_INFO *ginfo;
    I32 ret = IF_APP_ERROR;
    switch (code) {
        case CFG_WIRELESS:
            cli_app_log(CLI_LOG_INFO, "set wireless config \n");
            len = cmd_get_num_par(buf, Cell_Number, num);
            if (len == IF_APP_ERROR)
            {
                ret = IF_APP_ERROR;
                break;
            }
            tmp3 = atoi(num);
            tmp3--;            
            if ( (tmp3 >= 0 ) && (tmp3 < 7 )) 
            {
                lInfo = &wLte->lte[tmp3];
                ret = IF_APP_LTE1_CHNG + tmp3;
                cmd_get_num_par(buf, Cell_band, lInfo->band);

                cmd_get_num_par(buf, "ulEarfcn", lInfo->ul_earfcn);
                cmd_get_num_par(buf, "dlEarfcn", lInfo->dl_earfcn);
 /*
                if (tmp3 <= 5)
                {
                   cmd_get_num_par(buf, "freq", lInfo->ul_earfcn);
                   cmd_get_num_par(buf, "freq", lInfo->dl_earfcn);
                
                }*/
	
                cmd_get_num_par(buf, Cell_pci, lInfo->pci);
                cmd_get_num_par(buf, Cell_Number, num);
           //     cmd_get_num_par(buf, "freq", num);
    
	       
                bzero(num, DEV_INFO_NUM_LEN);
                cmd_get_num_par(buf, "pci", num);	
                bzero(num, DEV_INFO_NUM_LEN);
                cmd_get_num_par(buf, "plmnid", lInfo->plmnid);
                bzero(num, DEV_INFO_NUM_LEN);	
                cmd_get_num_par(buf, "tac", lInfo->tac);
                bzero(num, DEV_INFO_NUM_LEN);

                cmd_get_num_par(buf, "neighborCellList", data);
                cmd_get_num_par(data, "pci", lInfo->neiborcell);
                cmd_get_num_par(data, "freq", lInfo->abnorfreq);
                cmd_get_char_par(data, "freqList", lInfo->freqList);
                    
                
            }
            else if (tmp3 == 7 || tmp3 == 8)
            {
                if (tmp3 == 7)
                {
                    ginfo = &wGsm->gsm1;
                    ret = IF_APP_GSM1_CHNG;
                }
                else
                {
                    ginfo = &wGsm->gsm2;
                    ret = IF_APP_GSM2_CHNG;    
                }   
                cli_app_log(CLI_LOG_NOTICE, "set gsm wireless info \n");
                cmd_get_num_par(buf, Bts_band, ginfo->band);
                cmd_get_num_par(buf, Bts_BCC, ginfo->bcc);
                cmd_get_num_par(buf, Bts_MCC, ginfo->mcc);
                cmd_get_num_par(buf, Bts_MNC, ginfo->mnc);
                cmd_get_num_par(buf, Bts_LAC, ginfo->lac);
                cmd_get_num_par(buf, Bts_NEI, ginfo->nei);
                cmd_get_num_par(buf, "workMode", ginfo->workMode);
                cmd_get_num_par(buf, "configMode", ginfo->configMode);
                cmd_get_num_par(buf, "lowatt", ginfo->lowatt);
                cmd_get_num_par(buf, "ci", ginfo->cnum);
                cmd_get_num_par(buf, "cro", ginfo->cro);
                cmd_get_num_par(buf, "freq900Start", ginfo->startFreq900);
                cmd_get_num_par(buf, "freq900End", ginfo->endFreq900);
                cmd_get_num_par(buf, "freq1800Start", ginfo->startFreq1800);
                cmd_get_num_par(buf, "freq1800End", ginfo->endFreq1800);
                cmd_get_num_par(buf, "powerLevel", ginfo->PowerLevel);
                if (cmd_get_num_par(buf, "upatt", ginfo->upatt) > 0)
                {
                    if (atoi( ginfo->upatt) < 10)
                        ret = IF_APP_ERROR;
                }
                cmd_get_num_par(buf, "captime", ginfo->captime);

            }
            else if (tmp3 == 9)
            {
                CDMA_INFO *cdma = &wCdma->cdma;
                cmd_get_num_par(buf, Bts_MCC, cdma->mcc);
                cmd_get_num_par(buf, Bts_MNC, cdma->mnc);
                cmd_get_num_par(buf, "pn", cdma->pn);
                cmd_get_num_par(buf, "sid", cdma->sid);
                cmd_get_num_par(buf, "nid", cdma->nid);
                cmd_get_num_par(buf, "bsid", cdma->bsid);
                cmd_get_num_par(buf, "regnum", cdma->regnum);
                cmd_get_num_par(buf, "workMode", cdma->workModel);
                cmd_get_num_par(buf, "freq1", cdma->freq1);
                cmd_get_num_par(buf, "freq1Mode", cdma->workMode1);
                cmd_get_num_par(buf, "freq1ScanTime", cdma->scantime1);
                cmd_get_num_par(buf, "freq1ScanPeriod", cdma->scancaptime1);
                cmd_get_num_par(buf, "freq1neighborfreq1", cdma->neibor1Freq1);
                cmd_get_num_par(buf, "freq1neighborfreq2", cdma->neibor2Freq1);
                cmd_get_num_par(buf, "freq1neighborfreq3", cdma->neibor3Freq1);
                cmd_get_num_par(buf, "freq1neighborfreq4", cdma->neibor4Freq1);
                cmd_get_num_par(buf, "freq2", cdma->freq2);
                cmd_get_num_par(buf, "freq2Mode", cdma->workMode2);
                cmd_get_num_par(buf, "freq2ScanTime", cdma->scantime2);
                cmd_get_num_par(buf, "freq2ScanPeriod", cdma->scancaptime2);
                cmd_get_num_par(buf, "freq2neighborfreq1", cdma->neibor1Freq2);
                cmd_get_num_par(buf, "freq2neighborfreq2", cdma->neibor2Freq2);
                cmd_get_num_par(buf, "freq2neighborfreq3", cdma->neibor3Freq2);
                cmd_get_num_par(buf, "freq2neighborfreq4", cdma->neibor4Freq2);
                cmd_get_num_par(buf, "freq3", cdma->freq3);
                cmd_get_num_par(buf, "freq3Mode", cdma->workMode3);
                cmd_get_num_par(buf, "freq3ScanTime", cdma->scantime3);
                cmd_get_num_par(buf, "freq3ScanPeriod", cdma->scancaptime3);
                cmd_get_num_par(buf, "freq3neighborfreq1", cdma->neibor1Freq3);
                cmd_get_num_par(buf, "freq3neighborfreq2", cdma->neibor2Freq3);
                cmd_get_num_par(buf, "freq3neighborfreq3", cdma->neibor3Freq3);
                cmd_get_num_par(buf, "freq3neighborfreq4", cdma->neibor4Freq3);
                cmd_get_num_par(buf, "freq4", cdma->freq4);
                cmd_get_num_par(buf, "freq4Mode", cdma->workMode4);
                cmd_get_num_par(buf, "freq4ScanTime", cdma->scantime4);
                cmd_get_num_par(buf, "freq4ScanPeriod", cdma->scancaptime4);
                cmd_get_num_par(buf, "freq4neighborfreq1", cdma->neibor1Freq4);
                cmd_get_num_par(buf, "freq4neighborfreq2", cdma->neibor2Freq4);
                cmd_get_num_par(buf, "freq4neighborfreq3", cdma->neibor3Freq4);
                cmd_get_num_par(buf, "freq4neighborfreq4", cdma->neibor4Freq4);
                ret = IF_APP_CDMA_PARAM_CFG;
                

            }
            else if (tmp3 == 10)
            {
                WCDMA_INFO *wcdma = &wWcdma->wcdma;
                cmd_get_num_par(buf, Bts_MCC, wcdma->mcc); 
                cmd_get_num_par(buf, Bts_MNC, wcdma->mnc);
                cmd_get_num_par(buf, Bts_LAC, wcdma->lac);
                cmd_get_num_par(buf, "psc", wcdma->psc);
                cmd_get_num_par(buf, "arfcn", wcdma->arfcn);
                cmd_get_num_par(buf, "rac", wcdma->rac);
                cmd_get_num_par(buf, "cellId", wcdma->cellId);
                cmd_get_num_par(buf, "autoLac", wcdma->autoLac);
                cmd_get_num_par(buf, "dac", wcdma->dac);
                cmd_get_num_par(buf, "redirect", wcdma->redirect);
                cmd_get_num_par(buf, "bcc", wcdma->bcc);
                ret = IF_APP_WCDMA_PARAM_CFG ;
            }
            else if (tmp3 == 11)
            {
                TDSCDMA_INFO *tdscdma = &wTdscdma->tdscdma;
                cmd_get_num_par(buf, Bts_MCC, tdscdma->mcc); 
                cmd_get_num_par(buf, Bts_MNC, tdscdma->mnc);
                cmd_get_num_par(buf, Bts_LAC, tdscdma->lac);
                cmd_get_num_par(buf, "psc", tdscdma->psc);
                cmd_get_num_par(buf, "arfcn", tdscdma->arfcn);
                cmd_get_num_par(buf, "rac", tdscdma->rac);
                cmd_get_num_par(buf, "cellId", tdscdma->cellId);
                cmd_get_num_par(buf, "autoLac", tdscdma->autoLac);
                cmd_get_num_par(buf, "dac", tdscdma->dac);
                cmd_get_num_par(buf, "redirect", tdscdma->redirect);
                cmd_get_num_par(buf, "bcc", tdscdma->bcc);
                ret = IF_APP_TDSCDMA_PARAM_CFG ;
            }
            else
                return IF_APP_ERROR;

          
            break;

        case CFG_DET_INTVL:
            len = cmd_get_num_par(buf,Cell_Number, num);
            if (len == IF_APP_ERROR)
            {
                cmd_get_num_par(buf, cap_itvl, wGsm->intvl);
                ret = IF_APP_CAP_INTERVAL;
            }
            else
            {
                if (atoi(num) == 8)
                {
                    cmd_get_num_par(buf, cap_itvl, wGsm->intvl);
                    cli_app_log(CLI_LOG_NOTICE, "set wireless interval is %s \n", wGsm->intvl);
                    ret = IF_APP_CAP_INTERVAL;
                }
                else if (atoi(num) == 10)
                {
                    cmd_get_num_par(buf, cap_itvl, wCdma->intvl);
                    cli_app_log(CLI_LOG_NOTICE, "set wireless interval is %s \n", wCdma->intvl);
                    ret = IF_APP_CDMA_CAP_INTVL;
                }
                else if (atoi(num) == 11)
                {
                    cmd_get_num_par(buf, cap_itvl, wWcdma->intvl);
                    cli_app_log(CLI_LOG_NOTICE, "set wireless interval is %s \n", wWcdma->intvl);
                    ret = IF_APP_WCDMA_CAP_INTVL;
                }
                else if (atoi(num) == 12)
                {
                    cmd_get_num_par(buf, cap_itvl, wTdscdma->intvl);
                    cli_app_log(CLI_LOG_NOTICE, "set wireless interval is %s \n", wTdscdma->intvl);
                    ret = IF_APP_TDSCDMA_CAP_INTVL;
                }
                else if (atoi(num) == 13)
                {
                    cmd_get_num_par(buf, cap_itvl, wWifi->intvl);
                    cli_app_log(CLI_LOG_NOTICE, "set wireless interval is %s \n", wWifi->intvl);
                    ret = IF_APP_WIFI_CAP_INTVL;
                }
            }
            
            
            break;

        case CFG_FTP_INFO:
            cmd_get_char_par(buf, "ftpserver", ftpInfo->ftpserver);
            cmd_get_char_par(buf, "ftpuser", ftpInfo->ftpuser);
            cmd_get_char_par(buf, "ftppasswd", ftpInfo->ftppasswd);
            cmd_get_char_par(buf, "ftpport", ftpInfo->ftpport);
            cmd_get_char_par(buf, "ftpbsic", ftpInfo->ftpbsic);
            if (cmd_get_char_par(buf, "ftptimer", ftpInfo->ftptimer) > IF_APP_OK)
            {
                if (atoi(ftpInfo->ftptimer) < 2)
                {
                    cli_app_log(CLI_LOG_WARNING, "ftptimer must > 2 \n");
                    break;
                }
            }
            if (cmd_get_char_par(buf, "ftpmaxcount", ftpInfo->ftpmaxcount) > IF_APP_OK)
            {
                if (atoi(ftpInfo->ftpmaxcount) < 50)
                {
                    cli_app_log(CLI_LOG_WARNING, "max count < 50 \n");
                    break;
                }
            }
            cmd_get_char_par(buf, "ftpcityNumber", ftpInfo->ftpcityNum);
            ret = IF_APP_FTP_INFO;
            break;

        case CFG_SR_DATA_CYCLE:
            cmd_get_num_par(buf, sr_cycle, srvInfo->cycle);
            cli_app_log(CLI_LOG_WARNING, "set mng server cycle is %s \n", srvInfo->cycle);
            ret = IF_APP_DATA_CYCLE;           
            break;

        case CFG_UPDATA_TAC_CYCLE:             
            cmd_get_num_par(buf, tac_cycle, wLte->tac_cycle);
            cli_app_log(CLI_LOG_WARNING, "set tac cycle is %s \n", wLte->tac_cycle);
            ret = IF_APP_UPDATA_TAC_CYCLE;
            break;

       case CFG_DEVICE_MODE:
            len = cmd_get_num_par(buf, "deviceMode",  workMode->workMode);
             if (len > IF_APP_OK)
             {
                 cli_app_log(CLI_LOG_INFO, "set deviceMode is %s \n", workMode->workMode);
                 ret = IF_APP_DEVICE_MODE;
             }
             break;

       case CFG_CELL_UPDATE:
            len = cmd_get_num_par(buf, Cell_Number, num);
            if (len == IF_APP_ERROR)
            {
                ret = IF_APP_ERROR;
                break;
            }
            tmp3 = atoi(num);
            tmp3--;
            if ( (tmp3 >= 0 ) && (tmp3 < 7 )) 
            {
                lInfo = &wLte->lte[tmp3];
                ret = IF_APP_LTE1_CHNG + tmp3;
            }
            else 
            	return IF_APP_ERROR;
            cmd_get_num_par(buf, "plmnid", lInfo->plmnid);
            if (tmp3 <= 5)
            {
                cmd_get_num_par(buf, "freq", lInfo->ul_earfcn);
                cmd_get_num_par(buf, "freq", lInfo->dl_earfcn);
            }
            else
                cmd_get_num_par(buf, "freq", lInfo->dl_earfcn);
            cmd_get_num_par(buf, "tac", lInfo->tac);
            break;

        case CFG_WORK_PERIOD:
            cmd_get_num_par(buf, "workPeriod", workMode->workPeriod);
            cmd_get_num_par(buf, "powerOffTime", workMode->powerOffTime);
            if((atoi(workMode->workPeriod) - 2) < atoi(workMode->powerOffTime))  
                 break;         
            ret = IF_APP_WORK_PERIOD;
            break;

        case CFG_TOTAL_CELL:
            len = cmd_get_num_par(buf, Cell_Number, num);
            if (len == IF_APP_ERROR)
            {
                ret = IF_APP_ERROR;
                break;
            }
            tmp3 = atoi(num);
            tmp3--;
            if ( (tmp3 >= 0 ) && (tmp3 < 6)) 
            {
                lInfo = &wLte->lte[tmp3];
                ret = IF_APP_LTE1_CHNG + tmp3;
            }
            else 
            	return IF_APP_ERROR;
            
            //cmd_get_num_par(buf, Cell_band, info->band);

            cmd_get_num_par(buf, Cell_freq, lInfo->ul_earfcn);
            cmd_get_num_par(buf, Cell_freq, lInfo->dl_earfcn);
	
            cmd_get_num_par(buf, Cell_pci, lInfo->pci);
               
            cmd_get_num_par(buf, "bandwidth", lInfo->bandWidth);
            break;

        case CFG_POWER:
            break;

        case CFG_PA_GAIN:
            while(json_get_array(buf, data1, id) == 0)
            {

                memset(num, 0, DEV_INFO_NUM_LEN);
                cmd_get_num_par(data1, Cell_Number, num); 
                tmp3 = atoi(num);
                paInfo = (PA_INFO *)(rcvdata + sizeof(PA_INFO) * (atoi(num) -1 ));
                memset(num, 0, DEV_INFO_NUM_LEN);
                cmd_get_num_par(data1, "powerValue", num);
                strcpy(paInfo->gain, num);              
                cli_app_log(CLI_LOG_INFO, "set pa[%d] gain is %s \n", tmp3 , paInfo->gain);
                memset(data1, 0, DEV_INFO_ADD_LEN);
                id++;                           
            }
            ret = IF_APP_PA_GAIN;
            break;

        case CFG_AIR_SNIFFER:
            cmd_get_num_par(buf, snf_BccMode, snfInfo->bccMode);
            cmd_get_num_par(buf, snf_isAuto, snfInfo->isAuto);            
            len = cmd_get_char_par(buf, snf_runtime, data);
            cli_app_log(CLI_LOG_INFO, "auto sniffer data is %s \n", data);
            if ( len > IF_APP_OK)
            {
              
                cmd_get_num_par(data, snf_hour, snfInfo->hour);
                cmd_get_num_par(data, snf_min, snfInfo->min);
                cmd_get_num_par(data, snf_sec, snfInfo->sec);                


            }
            ret = IF_APP_SNIF_PAR;            
            break;

      case CFG_GPS_DELAY:
            while(json_get_array(buf, data1, id) == 0)
            {
                id++;
                len = cmd_get_num_par(data1, Cell_Number, num);
                if (len == IF_APP_ERROR)
                {
                    ret = IF_APP_ERROR;
                    break;
                }
                tmp3 = atoi(num);
                tmp3--;
                if ( (tmp3 >= 0 ) && (tmp3 <= 6)) 
                {
                    lInfo = &wLte->lte[tmp3];
                    cmd_get_num_par(data1, "GpsDelay", lInfo->gpsTmDelay);
                    cmd_get_num_par(data1, "IsMeasureEnable", lInfo->IsMeasureEnable);
                    cmd_get_num_par(data1, "BandWidth", lInfo->bandWidth);
                    cmd_get_num_par(data1, "SyncMode", lInfo->syncMode);
                    cmd_get_num_par(data1, "IsSaveFs", lInfo->isSaveFs);
                    cmd_get_num_par(data1, "minRxLev", lInfo->minRxLev);
                    cmd_get_num_par(data1, "minRxLevOffset", lInfo->minRxLevOffset);
                    cmd_get_char_par(data1, "configMode", lInfo->configMode);
                    cli_app_log(CLI_LOG_NOTICE, "set bbu[%d] gpsDelay is %s \n", tmp3 + 1,  lInfo->gpsTmDelay);
                    ret = IF_APP_GPS_DELAY;
                }
               memset(data1, 0, DEV_INFO_ADD_LEN);
           }
           break;

        case CFG_UEID_FILTER_ON:
             len = cmd_get_num_par(buf, Cell_Number, num);
             if (len > IF_APP_OK)
             {
                 *(I32 *)rcvdata = atoi(num);
                 memset(num, 0, DEV_INFO_NUM_LEN);
                 cmd_get_num_par(buf, "intvl", num);
                 *(I32 *) (rcvdata + 4) = atoi(num);
                 cli_app_log(CLI_LOG_INFO, "set filter bbu[%d] intlv is %d \n", *(I32 *)rcvdata, *(I32 *)(rcvdata + 4));
                 ret =  IF_APP_UE_FILTER_ON;
             }
            break;
 
        case CFG_UEID_FILTER_OFF:
             len = cmd_get_num_par(buf, Cell_Number, num);
             if (len > IF_APP_OK)
             {
                 ret = IF_APP_UE_FILTER_OFF;
                 *(I32 *)rcvdata = atoi(num);
             }
             break;

        case CFG_BASE:
            cmd_get_char_par(buf, devNumber, devInfo->devNum);
            
            cmd_get_char_par(buf, devName, devInfo->devName);
            cmd_get_char_par(buf, typeModel, devInfo->typemodel);
            cmd_get_char_par(buf, phoneNumber, devInfo->phoneNum); 
            cmd_get_char_par(buf, devPos, data);
            cmd_get_num_par(data, height, devInfo->pos_he);
            cmd_get_num_par(data, longitude, devInfo->pos_lo);
            cmd_get_num_par(data, latitude, devInfo->pos_la);
            cmd_get_char_par(buf, devAddresss, devInfo->devAddr);
            cmd_get_char_par(buf, "siteArea", devInfo->siteArea);
            ret = IF_APP_INFO_CHNG;
            break;

        case CFG_INTERFACE:
            srvInfo = (SRV_INFO *)rcvdata;
            cmd_get_char_par(buf, auth_ip, srvInfo->addr);
            cmd_get_num_par(buf, auth_port, srvInfo->port);
            srvInfo = (SRV_INFO *)(rcvdata + sizeof(SRV_INFO));
            cmd_get_char_par(buf, mng_ip, srvInfo->addr);
            cmd_get_num_par(buf, mng_port, srvInfo->port);
            srvInfo = (SRV_INFO *)(rcvdata + sizeof(SRV_INFO) * 2);
            cmd_get_char_par(buf, data_ip, srvInfo->addr);
            cmd_get_num_par(buf, data_port, srvInfo->port);
            ret = IF_APP_SRV_CHANGE;
            break;

        case CFG_STATUS_INTVL:
            cmd_get_num_par(buf, status_itvl, srvInfo->itvl);
            if ( atoi(srvInfo->itvl) < 5 )
                break;
            ret = IF_APP_MNG_H_CHNG;
            break;


        case CFG_DATA_INTVL:
            cmd_get_num_par(buf, data_itvl, srvInfo->itvl);
            if ( atoi(srvInfo->itvl) < 5 )
                break;
            ret = IF_APP_DATA_H_CHNG;
            break;

        case CFG_STOR:
            cmd_get_num_par(buf, save_time, storage->peroid);
            ret = IF_APP_STORAGE_SAVETIME;
            break;

        case CFG_SOFTWARE_UPDATA:

            tmp1 = (I8 *) rcvdata;
            //memset(tmp1, 0, sizeof(DEV_CONF));
            cmd_get_char_par(buf, updata_url, tmp1);
            cmd_get_char_par(buf, patchInfo, data);
            tmp1 += MD5_OFFSET;
            cmd_get_char_par(data, update_md5, tmp1);
            ret = IF_APP_SOFTWARE_UPDATE;

            break;
        case CFG_DATA_UP:
            tmp2 = (I32 *) rcvdata;	
				
            tmp3 = cmd_get_num_par(buf, startTime, num);
            if (tmp3 != IF_APP_ERROR)
            {
                cmd_convert_par(num, tmp2);
            }
			
            tmp2++;
            tmp3 = cmd_get_num_par(buf, endTime, num);
            if (tmp3 != IF_APP_ERROR)
            {
                cmd_convert_par(num, tmp2);
            }
			
            ret = IF_APP_REPEAT_DATAUP;
            break;

        case CFG_SMS_KEY:
             len = cmd_get_num_par(buf, smsMgrKey, smsInfo->smsKey);
             if (len > IF_APP_OK)
                ret = IF_APP_SMS_KEY;
             break;

        case CFG_BLACKLIST_ADD:
            len = cmd_get_char_par(buf, "imsi", rcvdata);
            if (len <= IF_APP_OK)
                break;
            else
                ret = IF_APP_ADD_BLACKLIST;
            break;

        case CFG_BLACKLIST_DEL:
            len = cmd_get_char_par(buf, "imsi", rcvdata);
            if (len <= IF_APP_OK)
                break;
            else
                ret = IF_APP_DEL_BLACKLIST;
            break;

        case CFG_RESTART_TIME:
            cmd_get_num_par(buf, "enable", restartInfo->enable);
            cmd_get_num_par(buf, "hour", restartInfo->hour);
            cmd_get_num_par(buf, "minute", restartInfo->min);
            ret = IF_APP_CFG_RESTART_TIME;
            break;

        case CFG_NTP_SERVER:
            {
                NTP_SERVER ntpserver;
                memset(&ntpserver, 0, sizeof(NTP_SERVER));
                ntpserver.num = 0;
                while(json_get_array(buf, data, id) == 0)
                {
                    id++;
                    len = cmd_get_char_par(data, "ntpServer", ntpserver.server[ntpserver.num]);
                    if (len == IF_APP_ERROR)
                    {
                        ret = IF_APP_ERROR;
                        break;
                    }
                    ntpserver.num++;
                    memset(data, 0, DEV_INFO_ADD_LEN);
                }
                if (cmd_set_ntp_server(ntpserver) != IF_APP_ERROR)
                {
                    ret = IF_APP_REBOOT;
                }
                else
                    ret = IF_APP_ERROR;
                break;
            }

        case CFG_DATA_CLEAN:
            cli_app_log(CLI_LOG_CRITICAL, "clean device data \n");
            ret = IF_APP_CLEAN_DATA;
            break;            

		case CFG_ALL_BBU_CONFIG:
			{
				WIRELESS_LTE_INFO *wLteTmp = (WIRELESS_LTE_INFO *)rcvdata;
				WIRELESS_GSM_INFO *wGsmTmp = (WIRELESS_GSM_INFO *)(rcvdata + sizeof(WIRELESS_LTE_INFO));
				cli_app_log(CLI_LOG_INFO, "config data is %s \n", buf);
				while(json_get_array(buf, data1, id) == 0)
				{
					id++;
					len = cmd_get_num_par(data1, Cell_Number, num);
					if (len == IF_APP_ERROR)
					{
						cli_app_log(CLI_LOG_ERROR, "can not find cellNumber in %s \n", data1);
						ret = IF_APP_ERROR;
						break;
					}
					tmp3 = atoi(num);
                
					if ( (tmp3 >= 1 ) && (tmp3 <= 7)) 
					{
						tmp3--;
						lInfo = &wLteTmp->lte[tmp3];
						cmd_get_num_par(data1, "GpsDelay", lInfo->gpsTmDelay);
						cmd_get_num_par(data1, "IsMeasureEnable", lInfo->IsMeasureEnable);
						cmd_get_num_par(data1, "BandWidth", lInfo->bandWidth);
						cmd_get_num_par(data1, "SyncMode", lInfo->syncMode);
						cmd_get_num_par(data1, "IsSaveFs", lInfo->isSaveFs);
						cmd_get_num_par(data1, Cell_band, lInfo->band);

						cmd_get_num_par(data1, "ulEarfcn", lInfo->ul_earfcn);
						cmd_get_num_par(data1, "dlEarfcn", lInfo->dl_earfcn);
						cmd_get_num_par(data1, Cell_pci, lInfo->pci);
						cmd_get_num_par(data1, "plmnid", lInfo->plmnid);
						cmd_get_num_par(data1, "tac", lInfo->tac);
						cmd_get_num_par(data1, "freqList", lInfo->freqList);
                        cmd_get_num_par(data1, "neighborCellList", data);
                        cmd_get_num_par(data1, "minRxLev", lInfo->minRxLev);
                        cmd_get_num_par(data1, "minRxLevOffset", lInfo->minRxLevOffset);
                        cmd_get_char_par(data1, "configMode", lInfo->configMode);
						if (tmp3 > 4)
						{
							cmd_get_char_par(data1, "ctFreqList", lInfo->ctFreqList);
						}
						cli_app_log(CLI_LOG_NOTICE, "set bbu[%d] gpsDelay is %s band %s ul-earfcn %s pci %s  neighborCellList %s ferqlist %s\n", tmp3 + 1,  
						lInfo->gpsTmDelay, lInfo->band, lInfo->ul_earfcn, lInfo->pci, data, lInfo->freqList);
						ret = IF_APP_CFG_ALL_BBU_CONFIG;
					}
					if (tmp3 == 8 || tmp3 == 9)
					{
						if (tmp3 == 8)
						{
							ginfo = &wGsmTmp->gsm1;
							ret = IF_APP_CFG_ALL_BBU_CONFIG;
						}
						else
						{
							ginfo = &wGsmTmp->gsm2;
							ret = IF_APP_CFG_ALL_BBU_CONFIG;    
						}   
						cli_app_log(CLI_LOG_NOTICE, "set gsm wireless info \n");
						cmd_get_num_par(data1, Bts_band, ginfo->band);
						cmd_get_num_par(data1, Bts_BCC, ginfo->bcc);
						cmd_get_num_par(data1, Bts_MCC, ginfo->mcc);
						cmd_get_num_par(data1, Bts_MNC, ginfo->mnc);
						cmd_get_num_par(data1, Bts_LAC, ginfo->lac);
						cmd_get_num_par(data1, "workMode", ginfo->workMode);
						cmd_get_num_par(data1, "configMode", ginfo->configMode);
						cmd_get_num_par(data1, "lowatt", ginfo->lowatt);
						cmd_get_num_par(data1, "ci", ginfo->cnum);
						cmd_get_num_par(data1, "cro", ginfo->cro);
						cmd_get_num_par(data1, "freq900Start", ginfo->startFreq900);
						cmd_get_num_par(data1, "freq900End", ginfo->endFreq900);
						cmd_get_num_par(data1, "freq1800Start", ginfo->startFreq1800);
						cmd_get_num_par(data1, "freq1800End", ginfo->endFreq1800);

						if (cmd_get_num_par(data1, "upatt", ginfo->upatt) > 0)
						{
							if (atoi( ginfo->upatt) < 10)
								ret = IF_APP_ERROR;
						}


					}
                    cli_app_log(CLI_LOG_INFO, "config cell is %d \n", tmp3);
			
					memset(data1, 0, DEV_INFO_ADD_LEN);
					memset(data, 0, DEV_INFO_ADD_LEN);
				}


			}        
			break;

		case CFG_FDD_MODE:
			len = cmd_get_num_par(buf, "devType", devInfo->devtype);
			if (len > IF_APP_OK)
			{
				ret = IF_APP_FDD_MODE;
			}
			break;

      default:
			cli_app_log(CLI_LOG_WARNING, "unknow config command \n");
            break;
    }

    return ret;
}

static I32 cmd_handle_search_msg(I32 code, I8 * buf, I8 * data)
{
    I32 ret = IF_APP_ERROR;
    I32 len = IF_APP_ERROR;
    I8 num[DEV_INFO_PAR_LEN];
    bzero(num, DEV_INFO_PAR_LEN);
    I8 tmp[DEV_INFO_NUM_LEN] = {0};
    I8 rcvData[DEV_INFO_ADD_LEN] = {0};
    switch(code)
    {

        case SR_RRU_PA_NUMBER:
	        ret = IF_APP_EUTRAN_BBU_INFO;
            break;
        case SR_EUTRAN_VERSION:
            cmd_get_num_par(buf,Cell_Number,num);
            memcpy(data,num,strlen(num));
            data[strlen(num)] = '\0';
            ret = IF_APP_SR_EUTRAN_VERSION;
            break;
        case SR_CURRENT_FREQ:
            cmd_get_char_par(buf, data_string, num);
            cmd_get_num_par(num, Cell_Number, tmp);
            memcpy(data, tmp, strlen(tmp));
            data[strlen(tmp)] = '\0';
            if (atoi(tmp) < 1 || atoi(tmp) > 13 )
                break;
            ret = IF_APP_SR_CURRENT_FREQ;
            break; 
       	
        case SR_BBU_CFG:
            cmd_get_char_par(buf, data_string, num);
            cmd_get_num_par(num, Cell_Number, tmp);
            memcpy(data, tmp, strlen(tmp));
            data[strlen(tmp)] = '\0';
            if (atoi(tmp) < 1 || atoi(tmp) > 13 )
                break;
            ret = IF_APP_SR_BBU_CFG;
            break;

        case SR_BBU_VERSION:
            cmd_get_char_par(buf, data_string, num);
            cmd_get_num_par(num, Cell_Number, tmp);
            memcpy(data, tmp, strlen(tmp));
            data[strlen(tmp)] = '\0';
            if (atoi(tmp) < 1 || atoi(tmp) > 13 )
                break;
            ret = IF_APP_SR_BBU_VERSION;
            break;

        case SR_TOTAL_CFG:
            ret = IF_APP_SR_TOTAL_CFG;
            break; 

        case SR_GPSINFO:
            ret = IF_APP_SR_GPSINFO;
            break;

        case SR_DEV_VERSION:
            ret = IF_APP_SR_DEV_VERSION;
            break;
        case SR_FTP_INFO:
            ret = IF_APP_SR_FTP_INFO;
            break;
        case SR_DEVICE_LOG:
            len = cmd_get_char_par(buf, "data", rcvData);
            cli_app_log(CLI_LOG_INFO , "search log data is %s \n", rcvData);
            if (len <= IF_APP_OK)
                break;
            len = cmd_get_num_par(rcvData, "ftpServer", ftpLog.server);
            if (len <= IF_APP_OK )
                break;
            len = cmd_get_num_par(rcvData, "ftpPort", num);
            if (len <= IF_APP_OK )
                break;
            ftpLog.port = atoi(num);
            len = cmd_get_num_par(rcvData, "ftpUser", ftpLog.user);
            if (len <= IF_APP_OK )
                break;
            len = cmd_get_num_par(rcvData, "ftpPassword", ftpLog.passwd);
            if (len <= IF_APP_OK )
                break;
            ftpLog.times = 0;
            ret = IF_APP_SR_DEVICE_LOG;
            break;
        
        case SR_BLACKLIST:
            ret = IF_APP_SR_BLACKLIST;
            break;

        case SR_X86_CFG:
            ret = IF_APP_SR_X86_CFG;
            break;
        case SR_X86_CURRENT_TIME:
            ret = IF_APP_SR_X86_CURRENT_TIME;
            break;

        case SR_RESTART_TIME:
            ret = IF_APP_SR_RESTART_TIME;
            break; 

        case SR_NTP_SERVER:
            ret = IF_APP_SR_NTP_SERVER;
            break;
    
        case SR_GSM_SNIFFER_TIME:
            ret = IF_APP_SR_GSM_SNIFFER_TIME;
            break;
			
		case SR_HW_ID:
			ret = IF_APP_SR_HW_ID;
			break;

		case SR_SERVER_RESART_SNF:
			ret = IF_APP_SR_SERVER_RESART_SNF;
			break;

		case SR_BOARDCARD_INFO:
			cmd_get_char_par(buf, data_string, num);
			cmd_get_num_par(num, Cell_Number, tmp);
            memcpy(data, tmp, strlen(tmp));
            data[strlen(tmp)] = '\0';
            if (atoi(tmp) < 1 || atoi(tmp) > 13 )
                break;
			ret = IF_APP_SR_BOARDCARD_INFO;
			break;
			
        case SR_TIME_REPEAT:
            cmd_get_char_par(buf, data_string, num);
            cmd_get_num_par(num, Cell_Number, tmp);
            memcpy(data, tmp, strlen(tmp));
            data[strlen(tmp)] = '\0';
            if (atoi(tmp) < 1 || atoi(tmp) > 13 )
                break;
            ret = IF_APP_SR_TIME_REPEAT;
            break;

        case SEARCH_ONE_RECORD:
            {
                SEARCHRECORD *record = (SEARCHRECORD *)data;
                cmd_get_char_par(buf, data_string, num);
                cmd_get_num_par(num, "seqNum", tmp);
                cmd_get_char_par(num, "date", record->date);
                record->seqNum = atoi(tmp);
                ret = IF_APP_SR_ONE_RECORD;
            }
            break;

        case SR_ONE_DAY_RECORD:
            cmd_get_char_par(buf, data_string, num);
            cmd_get_char_par(num, "date", data);
            ret = IF_APP_SR_ONE_DAY_RECORD;
            break;

        case SR_50_DAY_RECORD:
            ret = IF_APP_SR_50_DAY_RECORD;
            break;

        case SR_TAC_CYCLE:
            ret = IF_APP_SR_TAC_CYCLE;
            break;

        case SR_RECORD_BY_SEQ:
            cmd_get_char_par(buf, data_string, num);
            cmd_get_num_par(num, "seqNum", tmp);
            *(U16 *)data = atoi(tmp);
            ret = IF_APP_SR_RECORD_BY_SEQ;
            break;
            
        case SR_RECORD_END:
            ret = IF_APP_SR_RECORD_END;
            break;

        case SR_DEVICE_USAGE:
            {
                cmd_get_num_par(buf, data_string, num);
                SR_DATE *date = (SR_DATE*)data;
                if (cmd_get_num_par(num, "startDate", date->startDate) < 0)
                    break;

                if (cmd_get_num_par(num, "endDate", date->endDate) < 0)
                    break;
                cli_app_log(CLI_LOG_INFO, "start date is %s end date %s \n", date->startDate, date->endDate);
                ret = IF_APP_DEVICE_USAGE;

            }
            break;

        case SR_LOCATION_STATUS:
            ret = IF_APP_SR_LOCATION_STATUS;
            break;

        case SR_ALL_BBU_CONFIG:
            ret = IF_APP_SR_ALL_BBU_CONFIG;
            break;

        case SEND_UPGRADE_PACKET:
            {
                ZHONGDUN_PACKET *packet = (ZHONGDUN_PACKET *)data;
                len = cmd_get_char_par(buf, "data", rcvData);
                cli_app_log(CLI_LOG_INFO , "upgrade packet data is %s \n", rcvData);
                if (len <= IF_APP_OK)
                    break;
                cmd_get_char_par(rcvData, "sn", tmp);
                packet->sn = atoi(tmp);
                memset(tmp , 0, DEV_INFO_NUM_LEN);
                cmd_get_num_par(rcvData, "length", tmp);
                packet->len = atoi(tmp);
                cmd_get_char_par(rcvData, "crc", packet->crc);
                cmd_get_char_par(rcvData, "packet", packet->data);
                ret = IF_APP_SEND_UPGRADE_PACKET;

            }
            break;

        case SEND_PACKET_END:
            ret = IF_APP_SEND_PACKET_END;
            break;

        default:
            break;
    }
    return ret;
}

static void gen_cell_config(I32 slotId, LTE_INFO lteInfo, PA_INFO paInfo, JSON wireless)
{
    JSON cell, neibor_list;
    cell = json_object_new_object();
    neibor_list = json_object_new_object();
    json_add_int(Cell_Number, slotId, cell);
    json_add_int("GpsDelay", atoi(lteInfo.gpsTmDelay), cell);
    json_add_int("IsMeasureEnable", atoi(lteInfo.IsMeasureEnable), cell);
    json_add_int("BandWidth", atoi(lteInfo.bandWidth), cell);
    json_add_int("SyncMode", atoi(lteInfo.syncMode), cell);
    json_add_int("IsSaveFs", atoi(lteInfo.isSaveFs), cell);
    json_add_int(Cell_band, atoi(lteInfo.band), cell);
    json_add_int("ulEarfcn", atoi(lteInfo.ul_earfcn), cell);
    json_add_int("dlEarfcn", atoi(lteInfo.dl_earfcn), cell);
    json_add_int(Cell_pci, atoi(lteInfo.pci), cell);
    json_add_string(Cell_plmnid, lteInfo.plmnid, cell);
    json_add_int(Cell_tac, atoi(lteInfo.tac), cell);
    cli_app_log(CLI_LOG_NOTICE, "freq %s pci %s \n",  lteInfo.abnorfreq, lteInfo.neiborcell);
    json_add_string("freq", lteInfo.abnorfreq, neibor_list);
    json_add_string("pci", lteInfo.neiborcell, neibor_list);
    if (strcmp(dev_conf.dev.protocol, CAIPIN) != 0)
    {
        json_add_int("powerValue", atoi(paInfo.gain), cell);
        json_add_string("freqList", lteInfo.freqList, cell);
        json_add_int("minRxLev", atoi(lteInfo.minRxLev), cell);
        json_add_int("minRxLevOffset", atoi(lteInfo.minRxLevOffset), cell);
        json_add_int("configMode", atoi(lteInfo.configMode), cell);
    }
    json_object_object_add(cell, "neighborCellList", neibor_list);

	if (strcmp(dev_conf.dev.protocol, LOCATION) == 0)
	{
		
		if (slotId > 5)
		{
			json_add_string("ctFreqList", lteInfo.ctFreqList, cell);
		}

	}
    json_type type = json_object_get_type(wireless);
    if (type == json_type_array)
    {
        json_object_array_add(wireless, cell);
    }
    else if (type == json_type_object)
    {
        json_object_object_add(wireless, "data", cell);
    
    }
}

static void gen_gsm_config(I32 slotId, GSM_INFO gsmInfo, JSON wireless)
{
    JSON cell;
    cell = json_object_new_object();
    json_add_int(Cell_Number, slotId, cell);
    json_add_int(Cell_band, atoi(gsmInfo.band), cell);
    json_add_int("mnc", atoi(gsmInfo.mnc), cell);
    json_add_int("bcc", atoi(gsmInfo.bcc), cell);
    json_add_int("mcc", atoi(gsmInfo.mcc), cell);
    json_add_int("lac", atoi(gsmInfo.lac), cell);
    json_add_int("cro", atoi(gsmInfo.cro), cell);
    json_add_int("upatt", atoi(gsmInfo.upatt), cell);
    json_add_int("workMode", atoi(gsmInfo.workMode), cell);
    json_add_int("configMode", atoi(gsmInfo.configMode), cell);
    json_add_int("ci", atoi(gsmInfo.cnum), cell);
    json_add_int("freq900Start", atoi(gsmInfo.startFreq900), cell);
    json_add_int("freq900End", atoi(gsmInfo.endFreq900), cell);
    json_add_int("freq1800Start", atoi(gsmInfo.startFreq1800), cell);
    json_add_int("freq1800End", atoi(gsmInfo.endFreq1800), cell);
    json_add_int("powerValue", atoi(gsmInfo.lowatt), cell);
    json_add_int("RFSwitch", atoi(gsmInfo.enable), cell);
    json_type type = json_object_get_type(wireless);    
    if (type == json_type_array)
    {
        json_object_array_add(wireless, cell);
    }
    else if (type == json_type_object)
    {
        json_object_object_add(wireless, "data", cell);
    
    }
}

static void gen_wcdma_config(I32 slotId, WCDMA_INFO wcdmaInfo, JSON wireless)
{
    JSON cell;
    cell = json_object_new_object();
    json_add_int(Cell_Number, slotId, cell);
    json_add_int("mnc", atoi(wcdmaInfo.mnc), cell);
    json_add_int("bcc", atoi(wcdmaInfo.bcc), cell);
    json_add_int("mcc", atoi(wcdmaInfo.mcc), cell);
    json_add_int("lac", atoi(wcdmaInfo.lac), cell);
    json_add_int("psc", atoi(wcdmaInfo.psc), cell);
    json_add_int("arfcn", atoi(wcdmaInfo.arfcn), cell);
    json_add_int("rac", atoi(wcdmaInfo.rac), cell);
    json_add_int("cellId", atoi(wcdmaInfo.cellId), cell);
    json_add_int("autoLac", atoi(wcdmaInfo.autoLac), cell);
    json_add_int("dac", atoi(wcdmaInfo.dac), cell);
    json_add_int("redirect", atoi(wcdmaInfo.redirect), cell);
    if (strcmp(dev_conf.dev.protocol, CAIPIN) != 0)
        json_add_int("powerValue", atoi(wcdmaInfo.txPower), cell);
    json_type type = json_object_get_type(wireless);
    if (type == json_type_array)
    {
        json_object_array_add(wireless, cell);
    }
    else if (type == json_type_object)
    {
        json_object_object_add(wireless, "data", cell);
    }
}

static void gen_cdma_config(I32 slotId, CDMA_INFO cdmaInfo, JSON wireless)
{
    JSON cell;
    cell = json_object_new_object();
    json_add_int(Cell_Number, slotId, cell);
    json_add_int("mcc", atoi(cdmaInfo.mcc), cell);
    json_add_int("mnc", atoi(cdmaInfo.mnc), cell);
    json_add_int("pn", atoi(cdmaInfo.pn), cell);
    json_add_int("sid", atoi(cdmaInfo.sid), cell);
    json_add_int("nid", atoi(cdmaInfo.nid), cell);
    json_add_int("bsid", atoi(cdmaInfo.bsid), cell);
    json_add_int("regnum", atoi(cdmaInfo.regnum), cell);
    json_add_int("workMode", atoi(cdmaInfo.workModel), cell);
    json_add_int("freq1", atoi(cdmaInfo.freq1), cell);
    json_add_int("freq1Mode", atoi(cdmaInfo.workMode1), cell);
    json_add_int("freq1ScanTime", atoi(cdmaInfo.scantime1), cell);
    json_add_int("freq1ScanPeriod", atoi(cdmaInfo.scancaptime1), cell);
    json_add_int("freq1neighborfreq1", atoi(cdmaInfo.neibor1Freq1), cell);
    json_add_int("freq1neighborfreq2", atoi(cdmaInfo.neibor2Freq1), cell);
    json_add_int("freq1neighborfreq3", atoi(cdmaInfo.neibor3Freq1), cell);
    json_add_int("freq1neighborfreq4", atoi(cdmaInfo.neibor4Freq1), cell);
    json_add_int("freq2", atoi(cdmaInfo.freq2), cell);
    json_add_int("freq2Mode", atoi(cdmaInfo.workMode2), cell);
    json_add_int("freq2ScanTime", atoi(cdmaInfo.scantime2), cell);
    json_add_int("freq2ScanPeriod", atoi(cdmaInfo.scancaptime2), cell);
    json_add_int("freq2neighborfreq1", atoi(cdmaInfo.neibor1Freq2), cell);
    json_add_int("freq2neighborfreq2", atoi(cdmaInfo.neibor2Freq2), cell);
    json_add_int("freq2neighborfreq3", atoi(cdmaInfo.neibor3Freq2), cell);
    json_add_int("freq2neighborfreq4", atoi(cdmaInfo.neibor4Freq2), cell);
    json_add_int("freq3", atoi(cdmaInfo.freq3), cell);
    json_add_int("freq3Mode", atoi(cdmaInfo.workMode3), cell);
    json_add_int("freq3ScanTime", atoi(cdmaInfo.scantime3), cell);
    json_add_int("freq3ScanPeriod", atoi(cdmaInfo.scancaptime3), cell);
    json_add_int("freq3neighborfreq1", atoi(cdmaInfo.neibor1Freq3), cell);
    json_add_int("freq3neighborfreq2", atoi(cdmaInfo.neibor2Freq3), cell);
    json_add_int("freq3neighborfreq3", atoi(cdmaInfo.neibor3Freq3), cell);
    json_add_int("freq3neighborfreq4", atoi(cdmaInfo.neibor4Freq3), cell);
    json_add_int("freq4", atoi(cdmaInfo.freq4), cell);
    json_add_int("freq4Mode", atoi(cdmaInfo.workMode4), cell);
    json_add_int("freq4ScanTime", atoi(cdmaInfo.scantime4), cell);
    json_add_int("freq4ScanPeriod", atoi(cdmaInfo.scancaptime4), cell);
    json_add_int("freq4neighborfreq1", atoi(cdmaInfo.neibor1Freq4), cell);
    json_add_int("freq4neighborfreq2", atoi(cdmaInfo.neibor2Freq4), cell);
    json_add_int("freq4neighborfreq3", atoi(cdmaInfo.neibor3Freq4), cell);
    json_add_int("freq4neighborfreq4", atoi(cdmaInfo.neibor4Freq4), cell);
    if (strcmp(dev_conf.dev.protocol, CAIPIN) != 0)
        json_add_int("powerValue", atoi(cdmaInfo.dnatt), cell);
    json_type type = json_object_get_type(wireless);
    if (type == json_type_array)
    {
        json_object_array_add(wireless, cell);
    }
    else if (type == json_type_object)
    {
        json_object_object_add(wireless, "data", cell);
    
    }    

}

static void gen_tdscdma_config(I32 slotId, TDSCDMA_INFO wcdmaInfo, JSON wireless)
{
    JSON cell;
    cell = json_object_new_object();
    json_add_int(Cell_Number, slotId, cell);
    json_add_int("mnc", atoi(wcdmaInfo.mnc), cell);
    json_add_int("mcc", atoi(wcdmaInfo.mcc), cell);
    json_add_int("lac", atoi(wcdmaInfo.lac), cell);
    json_add_int("psc", atoi(wcdmaInfo.psc), cell);
    json_add_int("arfcn", atoi(wcdmaInfo.arfcn), cell);
    json_add_int("rac", atoi(wcdmaInfo.rac), cell);
    json_add_int("cellId", atoi(wcdmaInfo.cellId), cell);
    json_add_int("autoLac", atoi(wcdmaInfo.autoLac), cell);
    json_add_int("dac", atoi(wcdmaInfo.dac), cell);
    json_add_int("redirect", atoi(wcdmaInfo.redirect), cell);
    json_add_int("bcc", atoi(wcdmaInfo.bcc), cell);
    if (strcmp(dev_conf.dev.protocol, CAIPIN) != 0)
        json_add_int("powerValue", atoi(wcdmaInfo.txPower), cell);
    json_type type = json_object_get_type(wireless);
    if (type == json_type_array)
    {
        json_object_array_add(wireless, cell);
    }
    else if (type == json_type_object)
    {
        json_object_object_add(wireless, "data", cell);
    }
}

static I32 cmd_gen_cell_search(I32 req, I8 *buf, I8 *data, DEV_CONF *dev_conf)
{
    JSON cell_search  ;  
    time_t rawtime ;
    I32 slotId = atoi(data), ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    if (req == REQUEST_SR_CELL_FREQ)
        json_add_int("code", SR_CURRENT_FREQ, cell_search);
    else
        json_add_int("code", SR_BBU_CFG, cell_search);      
    json_add_string("msg", "", cell_search);
    if (slotId <= 7 )
    	gen_cell_config(slotId, dev_conf->capLte.lte[slotId - 1],  dev_conf->pa[slotId - 1],cell_search);
    else if (slotId == 8)
        gen_gsm_config(slotId, dev_conf->capGsm.gsm1, cell_search);
    else if (slotId == 9)
        gen_gsm_config(slotId, dev_conf->capGsm.gsm2, cell_search);
    else if (slotId == 10)
        gen_cdma_config(slotId, dev_conf->capCdma.cdma, cell_search);
    else if (slotId == 11)
        gen_wcdma_config(slotId, dev_conf->capWcdma.wcdma, cell_search);
    else if (slotId == 12)
        gen_tdscdma_config(slotId, dev_conf->capTdscdma.tdscdma, cell_search);
    
	json_add_int(timestamp, rawtime, cell_search);
	ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;

}

static I32 cmd_gen_local_config_sotr(I8 *buf, DEV_CONF *dev_conf)
{
    I32 ret = IF_APP_OK;
    JSON stor, singel_root;
    time_t rawtime;
    time(&rawtime);
    stor = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_STOR, stor);
    json_add_int(save_time, atoi(dev_conf->storage.peroid), singel_root);
    json_add_int(searchtime, atoi(dev_conf->storage.peroid), singel_root);
    json_object_object_add( stor, data_string, singel_root);
    json_add_int(timestamp, rawtime, stor);
   
    ret = sprintf(buf, "%s", json_object_to_json_string(stor));
    json_object_put(stor);
    return ret ;
}

static I32 cmd_gen_local_config_data_intvl(I8 *buf, DEV_CONF *devConf)
{
    I32 ret = IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    JSON data_intvl, singel_root;
    data_intvl = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_DATA_INTVL, data_intvl);
    json_add_int(data_itvl, atoi(devConf->data.itvl), singel_root);
    json_object_object_add(data_intvl, data_string, singel_root);
    json_add_int(timestamp, rawtime, data_intvl);
    ret = sprintf(buf, "%s", json_object_to_json_string(data_intvl));
    json_object_put(data_intvl);
    return ret ;
}

static I32 cmd_gen_local_config_det_intvl(I8 *buf, DEV_CONF *dev_conf)
{
    I32 ret =IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    JSON det_intvl, singel_root;
    det_intvl = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_DET_INTVL, det_intvl);
    json_add_int(cap_itvl, atoi(dev_conf->capGsm.intvl), singel_root);
    json_object_object_add(det_intvl, data_string, singel_root);
    json_add_int(timestamp, rawtime, det_intvl);
    ret = sprintf(buf, "%s", json_object_to_json_string(det_intvl));
    json_object_put(det_intvl);
    return ret ;
}

static I32 cmd_gen_local_config_status_intvl(I8 *buf, DEV_CONF *devConf)
{
    I32 ret = IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    JSON status_intvl, singel_root;
    status_intvl = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_STATUS_INTVL, status_intvl);
    json_add_int(status_itvl, atoi(devConf->mng.itvl), singel_root);
    json_object_object_add(status_intvl, data_string, singel_root);
    json_add_int(timestamp, rawtime, status_intvl);
    ret = sprintf(buf, "%s", json_object_to_json_string(status_intvl));
    json_object_put(status_intvl);
    return ret ;

}


static I32 cmd_gen_local_config_data_cycle(I8 *buf, DEV_CONF *devConf)
{
    I32 ret = IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    JSON data_cycle, singel_root;
    data_cycle = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_SR_DATA_CYCLE, data_cycle);
    json_add_int("cycle", atoi(devConf->mng.cycle), singel_root);
    json_object_object_add(data_cycle, data_string, singel_root);
    json_add_int(timestamp, rawtime, data_cycle);
    ret = sprintf(buf, "%s", json_object_to_json_string(data_cycle));
    json_object_put(data_cycle);
    return ret ;
}

static I32 cmd_gen_local_config_interface(I8 *buf, DEV_CONF *devConf)
{
    I32 ret = IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    JSON interface, singel_root;
    interface = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_INTERFACE, interface);
    json_add_int(auth_port, atoi(devConf->auth.port), singel_root);
    json_add_string(auth_ip, devConf->auth.addr, singel_root);
    json_add_int(mng_port, atoi(devConf->mng.port), singel_root);
    json_add_string(mng_ip, devConf->mng.addr, singel_root);
    json_add_int(data_port, atoi(devConf->data.port), singel_root);
    json_add_string(data_ip, devConf->data.addr, singel_root);
    json_object_object_add(interface, data_string, singel_root);
    json_add_int(timestamp, rawtime, interface);

    ret = sprintf(buf, "%s", json_object_to_json_string(interface));
    json_object_put(interface);
    return ret ;
}



static I32 cmd_gen_local_config_wireless(I8 *buf, DEV_CONF* dev_conf)
{
    I32 ret = IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    JSON wireless, singel_root,  wireless_list;
    wireless = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_WIRELESS, wireless);
    I32 i = 0;

    wireless_list = json_object_new_array();
    if (atoi(dev_conf->capLte.isSupported) == 1)
    {
        for (i = LTE_CELL_START; i <= LTE_CELL_END; i++)
        {
            if (atoi(dev_conf->capLte.lte[i].enable) == 1)
            {
                gen_cell_config((i + 1), dev_conf->capLte.lte[i],  dev_conf->pa[i], wireless_list);
            }
        }
    }
    if (atoi(dev_conf->capGsm.isSupported) == 1)
    {
        
        gen_gsm_config((GSM_CELL_START + 1), dev_conf->capGsm.gsm1, wireless_list);
        gen_gsm_config((GSM_CELL_END + 1), dev_conf->capGsm.gsm2, wireless_list);
    }

	if (atoi(dev_conf->capCdma.isSupported) == 1)
	{
	    gen_cdma_config((GSM_CELL_END + 2), dev_conf->capCdma.cdma, wireless_list);
	}

	if (atoi(dev_conf->capWcdma.isSupported) == 1)
	{
	    gen_wcdma_config((WCDMA_CELL_START + 1), dev_conf->capWcdma.wcdma, wireless_list);
	}

    if (atoi(dev_conf->capTdscdma.isSupported) == 1)
    {
        gen_tdscdma_config((WCDMA_CELL_END + 1), dev_conf->capTdscdma.tdscdma, wireless_list);
    }

    json_object_object_add(singel_root, "wireless", wireless_list);
    json_object_object_add(wireless, data_string, singel_root);
    json_add_int(timestamp, rawtime, wireless);
    ret = sprintf(buf, "%s", json_object_to_json_string(wireless));
    json_object_put(wireless);
    return ret;
}


static void gen_sniffer_config(I32 slotId, DEV_CONF *dev_conf, JSON sniffer)
{
    JSON cell;
    cell = json_object_new_object();
    json_add_int(Cell_Number, slotId, cell);
    json_add_int(snf_hour, atoi(dev_conf->snfLte.snf_tm[slotId -1].hour), cell);
    json_add_int(snf_min, atoi(dev_conf->snfLte.snf_tm[slotId -1].min), cell);
    json_add_int(snf_sec, atoi(dev_conf->snfLte.snf_tm[slotId -1].sec), cell);
    json_object_array_add(sniffer, cell);

}


static I32 cmd_gen_local_config_sniffer(I8 *buf, DEV_CONF *dev_conf)
{
    JSON sniffer , singel_root , isauto, sniffer_list;
    I32 i = 0, ret = IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    sniffer_list = json_object_new_array();
    sniffer = json_object_new_object();
    singel_root = json_object_new_object();
    json_add_int(cfgcode, CFG_AIR_SNIFFER, sniffer);
    json_add_int(snf_BccMode, atoi(dev_conf->snfLte.bccMode), singel_root);
    if (atoi(dev_conf->snfLte.isAuto) == 1)
        isauto = json_object_new_boolean(1);
    else
        isauto = json_object_new_boolean(0);
    json_object_object_add(sniffer, snf_isAuto, isauto);
    for (i = 0; i < CELL_NUM; i++)
    {   
         if (atoi(dev_conf->capLte.lte[i].enable) == 1)
        {
            gen_sniffer_config((i + 1), dev_conf, sniffer_list);
        }

    }
    json_object_object_add(singel_root, "runTime", sniffer_list);
    json_object_object_add(sniffer, data_string,  singel_root);
    json_add_int(timestamp, rawtime, sniffer);
    ret = sprintf(buf, "%s", json_object_to_json_string(sniffer));
    json_object_put(sniffer);
    return ret;

}

static I32 cmd_gen_local_config_sms_key(I8 *buf, DEV_CONF* dev_conf)
{
    I32 ret = IF_APP_OK;
    JSON singel_root, smskey;
    time_t rawtime;
    time(&rawtime);
    singel_root = json_object_new_object();
    smskey = json_object_new_object();
    json_add_string(smsMgrKey, dev_conf->sms.smsKey, smskey);
    json_add_int(cfgcode, CFG_SMS_KEY, singel_root);
    json_object_object_add(singel_root, data_string, smskey);
    json_add_int(timestamp, rawtime, singel_root);
    ret = sprintf(buf, "%s", json_object_to_json_string(singel_root));
    cli_app_log(CLI_LOG_INFO, "SMS KEY is == %s \n", buf);
    json_object_put(singel_root);
    return ret;    
}

I32 cmd_gen_local_config_response(I32 type, I8 *buf, DEV_CONF* dev_conf)
{
    I32 ret = IF_APP_ERROR;
    if (buf == NULL)
        return ret ;
    if (type == 1)
        config_init++;
    if ((type == 1) && (config_init == LOCAL_CONFIG_END))
    {
        config_init = 1;
        return  LOCAL_CONFIG_END;
    }
    switch (config_init)
    {
        case LOCAL_CONFIG_SOTR :
            ret = cmd_gen_local_config_sotr(buf, dev_conf);
            break;
             
        case LOCAL_CONFIG_DATA_INTVL:
            ret = cmd_gen_local_config_data_intvl(buf, dev_conf);
            break;

        case LOCAL_CONFIG_DET_INTVL:
            ret = cmd_gen_local_config_det_intvl(buf, dev_conf);
            break;

        case LOCAL_CONFIG_STATUS_INTVL:	
            ret = cmd_gen_local_config_status_intvl(buf, dev_conf);
            break;
  
        case LOCAL_CONFIG_DATA_CYCLE :
            ret = cmd_gen_local_config_data_cycle(buf, dev_conf);
            break;

        case LOCAL_CONFIG_INTERFACE:
            ret = cmd_gen_local_config_interface(buf, dev_conf);
            break;

        case LOCAL_CONFIG_WIRELESS:
            ret = cmd_gen_local_config_wireless(buf, dev_conf);
            break;

        case LOCAL_CONFIG_SNIFFER:
            ret = cmd_gen_local_config_sniffer(buf, dev_conf);
            break;
       
        case LOCAL_CONFIG_SMS_KEY:
            ret = cmd_gen_local_config_sms_key(buf, dev_conf);
            break;

        default:
            break;

    }

    return ret;
}

I32 cmd_gen_all_bbu_config(DEV_CONF *dev_conf, I8 *buf)
{
    JSON cell_search, wireless_list ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();

    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_ALL_BBU_CONFIG, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    wireless_list = json_object_new_array();
    int i = 0;
    if (atoi(dev_conf->capLte.isSupported) == 1)
    {
        for (i = LTE_CELL_START; i <= LTE_CELL_END; i++)
        {
            if (atoi(dev_conf->capLte.lte[i].enable) == 1)
            {
                gen_cell_config((i + 1), dev_conf->capLte.lte[i],  dev_conf->pa[i], wireless_list);
            }
        }
    }
    if (atoi(dev_conf->capGsm.isSupported) == 1)
    {
        
        gen_gsm_config((GSM_CELL_START + 1), dev_conf->capGsm.gsm1, wireless_list);
        gen_gsm_config((GSM_CELL_END + 1), dev_conf->capGsm.gsm2, wireless_list);
    }
    json_object_object_add(cell_search, data_string, wireless_list);
    
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;



}

I32 cmd_gen_normal_response(I32 type, I32 code, I8 *buf)
{
    
    JSON cell_search  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", type, cell_search);
    json_add_int("code", code, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;

}

I32 cmd_gen_ftp_response(I32 type, I32 code, FTP_INFO info, I8 *buf)
{
    
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", type, cell_search);
    json_add_int("code", code, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    json_add_string("ftpserver", info.ftpserver, dataRoot);
    json_add_string("ftpuser", info.ftpuser, dataRoot);
    json_add_string("ftppasswd", info.ftppasswd, dataRoot);
    json_add_string("ftpport", info.ftpport, dataRoot);
    json_add_string("ftptimer", info.ftptimer, dataRoot);
    json_add_string("ftpmaxcount", info.ftpmaxcount, dataRoot);
    json_add_string("ftpcityNumber", info.ftpcityNum, dataRoot);
    json_add_string("ftpbsic", info.ftpbsic, dataRoot);
    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;

}


I32 cmd_gen_x86_config(DEV_CONF* dev_conf, I8 *buf)
{
    
    JSON cell_search, dataRoot, interface, base, devPos, store, dataUpload, statsReport, ftpInfo, restartInfo;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_X86_CFG, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    json_add_int("RFSwitch", atoi(dev_conf->dev.RFSwitch), dataRoot);

    interface = json_object_new_object();
    json_add_int("mgr1Port", atoi(dev_conf->mng.port), interface);
    json_add_string("mgr1SrvHost", dev_conf->mng.addr, interface);
    json_add_int("up1Port", atoi(dev_conf->data.port), interface);
    json_add_string("up1SrvHost", dev_conf->data.addr, interface);
    json_object_object_add(dataRoot, "interface", interface);
    
    base = json_object_new_object();
    json_add_string("devAddresss", dev_conf->dev.devAddr, base);
    json_add_int("devConformation", 1, base);
    json_add_string("devName", dev_conf->dev.devName, base);
    devPos = json_object_new_object();
    json_add_double("height", atof(dev_conf->dev.pos_he), devPos);
    json_add_double("latitude", atof(dev_conf->dev.pos_la), devPos);
    json_add_double("longitude", atof(dev_conf->dev.pos_lo), devPos);
    json_object_object_add(base, "devPos", devPos);
    json_add_int("devType", atoi(dev_conf->dev.devtype), base);
    json_add_string("phoneNumber", dev_conf->dev.phoneNum, base);
    json_add_string("typeModel", dev_conf->dev.typemodel, base);
    json_add_string("siteArea", dev_conf->dev.siteArea, base);
    json_object_object_add(dataRoot, "baseCfg", base);
    
    store = json_object_new_object();
    json_add_int("savetime", atoi(dev_conf->storage.peroid), store);
    json_object_object_add(dataRoot, "storeCfg", store);

    dataUpload = json_object_new_object();
    json_add_int("dataUpCycSeconds", atoi(dev_conf->data.itvl), dataUpload);
    json_add_int("uploadEnable", atoi(dev_conf->data.enable), dataUpload);
    json_object_object_add(dataRoot, "dataUpload", dataUpload);

    
    statsReport = json_object_new_object();
    json_add_int("statsCycSeconds", atoi(dev_conf->mng.itvl), statsReport);
    json_add_int("statsEnable", atoi(dev_conf->mng.enable), statsReport);
    json_object_object_add(dataRoot, "statsReport", statsReport);

    ftpInfo = json_object_new_object();
    json_add_string("ftpserver", dev_conf->ftp.ftpserver, ftpInfo);
    json_add_string("ftpuser", dev_conf->ftp.ftpuser, ftpInfo);
    json_add_string("ftppasswd", dev_conf->ftp.ftppasswd, ftpInfo);
    json_add_string("ftpcityNumber", dev_conf->ftp.ftpcityNum, ftpInfo);
    json_add_string("ftpbsic", dev_conf->ftp.ftpbsic, ftpInfo);
    json_add_int("ftptimer", atoi(dev_conf->ftp.ftptimer), ftpInfo);
    json_add_int("ftpEnable", atoi(dev_conf->ftp.ftpenable), ftpInfo);
    json_object_object_add(dataRoot, "ftpInfo", ftpInfo);

    restartInfo = json_object_new_object();
    json_add_int("enable", atoi(dev_conf->restart.enable), restartInfo);
    json_add_int("hour", atoi(dev_conf->restart.hour), restartInfo);
    json_add_int("minute", atoi(dev_conf->restart.min), restartInfo);
    json_object_object_add(dataRoot, "restartCfg", restartInfo);


    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;

}

static I32 cmd_gen_x86_current_time(I8 *buf)
{
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_X86_CURRENT_TIME, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    json_add_int("currentTime", rawtime, dataRoot);
    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;
}

static I32 cmd_gen_restart_time(RESTART_INFO restart, I8* buf)
{
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_RESTART_TIME, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    json_add_int("enable", atoi(restart.enable), dataRoot);
    json_add_int("hour", atoi(restart.hour), dataRoot);
    json_add_int("minute", atoi(restart.min), dataRoot);
    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;
}

static I32 cmd_gen_ntp_server(I8 *sendBuf)
{
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_array();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_NTP_SERVER, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);

    FILE *fd;
    fd = fopen(ntpServerFile, "r");
    if (fd == NULL)
    {
        cli_app_log(CLI_LOG_WARNING, "can not open file %s \n", ntpServerFile);
        json_add_int("result", RST_FAILED, cell_search);
        ret = sprintf(sendBuf, "%s", json_object_to_json_string(cell_search));
        json_object_put(cell_search);
        return ret;
    }
    struct stat fileStat;
    stat(ntpServerFile, &fileStat);
    cli_app_log(CLI_LOG_NOTICE, "file size if %d \n", fileStat.st_size);
    I8 buf[DEV_INFO_PAR2_LEN] = {0};
    I8 server[DEV_INFO_PAR_LEN] = {0};
    while(fgets(buf, 256, fd) != NULL)
    {
        if (strstr(buf, "list server"))
        {
            strcpy(server, strstr(buf, "list server") + 13);
            server[strlen(server) - 2] = '\0';
            cli_app_log(CLI_LOG_INFO, "server  is %s \n", server);
            json_add_array("ntpServer", server, dataRoot);
        }
        if (strstr(buf, "option enabled"))
        {
            cli_app_log(CLI_LOG_INFO, "ntp file end %s \n", buf);
            break;
        }
        memset(buf, 0, DEV_INFO_PAR2_LEN);
        memset(server, 0, DEV_INFO_PAR_LEN);
    }
    fclose(fd);

    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(sendBuf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;
}


static I32 cmd_gen_gsm_sniffer_time(SNF_INFO sniffer, I8* buf)
{
    JSON cell_search, dataRoot, dataRoot1;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    dataRoot1 = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_GSM_SNIFFER_TIME, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    json_add_int("autoBccMode", atoi(sniffer.bccMode), dataRoot);
    json_add_bool("isAuto", atoi(sniffer.isAuto), dataRoot);
    json_add_int("hour", atoi(sniffer.hour), dataRoot1);
    json_add_int("minute", atoi(sniffer.min), dataRoot1);
    json_add_int("second", atoi(sniffer.sec), dataRoot1);
    json_object_object_add(dataRoot, "runTime", dataRoot1);
    json_add_int("snifferCycle", atoi(sniffer.cycle), dataRoot);
    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;
}

static I32 cmd_gen_time_repeat(DEV_CONF *dev_conf, I8 *data, I8 *buf)
{
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_TIME_REPEAT, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    I32 slotId = atoi(data);
    
    json_add_int("cellNumber", slotId, dataRoot);
    if (slotId == 8 || slotId == 9)
    {
        json_add_int("reDetectMinuts", atoi(dev_conf->capGsm.intvl), dataRoot);
    }
    else if (slotId == 10)
    {
        json_add_int("reDetectMinuts", atoi(dev_conf->capCdma.intvl), dataRoot);
    }
    else if (slotId == 11)
    {
        json_add_int("reDetectMinuts", atoi(dev_conf->capWcdma.intvl), dataRoot);
    }
    else if (slotId == 12)
    {
        json_add_int("reDetectMinuts", atoi(dev_conf->capTdscdma.intvl), dataRoot);
    }
    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;
}

static I32 cmd_gen_tac_cycle(DEV_CONF *dev_conf, I8 *data, I8 *buf)
{
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_TAC_CYCLE, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int("tacUpdateCycle", atoi(dev_conf->capLte.tac_cycle), dataRoot);
    
    json_object_object_add(cell_search, "data", dataRoot);
    json_add_int(timestamp, rawtime, cell_search);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;
}

I32 cmd_gen_location_status(DEV_STATUS * status, I8 * buf, DEV_CONF *conf)
{
    JSON cell_search, dataRoot, cellWireless, cellActive;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR, i = 0;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_LOCATION_STATUS, cell_search);      
    json_add_string("msg", "", cell_search);
    cellWireless = json_object_new_array();
    cellActive = json_object_new_array();
    json_add_int("tddLocationEnable", status->tddLocationEnable, dataRoot);
    json_add_int("fddLocationEnable", status->fddLocationEnable, dataRoot);
    for (i = 0; i <7; i++)
    {
        if (atoi(conf->capLte.lte[i].enable) == 1)
        {
            if (conf->pa[i].enable[0] == '1' &&  status->realTimeStatus[i].isActive == 2 )
            {
                if (status->tddLocationEnable == 1 && i < 5)
                {
                    json_add_array2(Cell_Number, (i + 1), "RFSwitch", 1, cellWireless);
                }
                else if (status->fddLocationEnable == 1 && i > 4)
                {
                    json_add_array2(Cell_Number, (i + 1), "RFSwitch", 1, cellWireless);
                }
                else
                {
                    json_add_array2(Cell_Number, (i + 1), "RFSwitch", 0, cellWireless);
                }
            }
            else
            {

                json_add_array2(Cell_Number, (i + 1), "RFSwitch", 0, cellWireless);
            }
            json_add_array2(Cell_Number, (i + 1), "active", status->realTimeStatus[i].isActive, cellActive);
        }

    }
    json_object_object_add(dataRoot, "BbuRFState", cellWireless);
    json_object_object_add(dataRoot, "BbuActiveState", cellActive);
    
    json_object_object_add(cell_search, "data", dataRoot);
    json_add_int(timestamp, rawtime, cell_search);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;


}

I32 cmd_gen_sr_local_response(I32 type, I8 *buf, I8 *data, DEV_CONF* dev_conf)
{
    I32 ret = IF_APP_ERROR;
    if (buf == NULL)
       return IF_APP_ERROR;
    switch(type)
    {
        case REQUEST_SR_CELL_FREQ:
            ret = cmd_gen_cell_search(type, buf, data,dev_conf);
            break;

        case REQUEST_SR_BBU_CFG:
            ret = cmd_gen_cell_search(type, buf, data,dev_conf);
            break;

        case REQUEST_SR_TOTAL_CFG:
            ret = cmd_gen_normal_response(CMD_SEARCH, SR_TOTAL_CFG ,buf);
            break;
        case REQUEST_SR_FTP_INFO:
            ret = cmd_gen_ftp_response(CMD_SEARCH, SR_FTP_INFO, dev_conf->ftp, buf);
            break;
            
        case REQUEST_SR_TOTALCFG_MNG:
            ret = cmd_gen_total_bbu_info_string(dev_conf, buf);  
            break;

        case REQUEST_SR_X86_CFG:
            ret = cmd_gen_x86_config(dev_conf, buf);
            break;
        
        case REQUEST_SR_X86_CURRENT_TIME:
            ret = cmd_gen_x86_current_time(buf);
            break;

        case REQUEST_SR_RESTART_TIME:
            ret = cmd_gen_restart_time(dev_conf->restart, buf);
            break;

        case REQUEST_SR_NTP_SERVER:
            ret = cmd_gen_ntp_server(buf);
            break; 
        
        case REQUEST_SR_GSM_SNIFFER_TIME:
            ret = cmd_gen_gsm_sniffer_time(dev_conf->snfGsm, buf);
            break;

        case REQUEST_SR_TIME_REPEAT:
            ret = cmd_gen_time_repeat(dev_conf, data, buf);
            break;

        case REQUEST_SR_TAC_CYCLE:
            ret = cmd_gen_tac_cycle (dev_conf, data, buf);
            break;

        case REQUEST_SR_ALL_BBU_CONFIG:
            ret = cmd_gen_all_bbu_config(dev_conf,  buf);
            break;

		case REQUEST_SR_HW_ID:
			ret = cmd_gen_hw_id(buf);

		case REQUEST_SR_SERVER_RESART_SNF:
			ret = cmd_gen_server_restart_snf(dev_conf, buf);

		case REQUEST_SR_BOARDCARD_INFO:
			ret = cmd_gen_boardcard_info(dev_conf, data, buf);

        default:
           break;
    }
    return ret;

}

static void  cmd_build_bbu_state(I32 slotId, I32 isalive, I32 memusage, I32 cpuusage, I32 cputemp, I32 boardtemp, I32 synctype, I32 syncstate, JSON bbuState)
{
    JSON  singel_root;
    singel_root = json_object_new_object();
    json_add_int(Cell_Number, slotId, singel_root);
    json_add_int("state", isalive, singel_root);
    json_add_int("memusage", memusage, singel_root);
    json_add_int("cpuusage", cpuusage, singel_root);
    json_add_int("cputemp", cputemp, singel_root);
    json_add_int("boardtemp", boardtemp, singel_root);
    json_add_int("syncType", synctype, singel_root);
    json_add_int("syncState", syncstate, singel_root);
    json_object_array_add( bbuState, singel_root);
}

I32 cmd_gen_all_bbu_state(BBU_STATUS *bbu_status,I8 * buf, I32 *slotID)
{
    I32 ret = IF_APP_OK;
    I32 i = 0;
    JSON bbuState;
    bbuState = json_object_new_array();
   
    for ( ; i < 7; i ++)
    {
        if ((slotID[i] == 7) || (slotID[i] == -1))
            cmd_build_bbu_state((i + 1), 0, 0, 0, 0, 0, 0, 0, bbuState);

        else
            cmd_build_bbu_state((i + 1), 1, bbu_status[i].memusage, bbu_status[i].cpuusage, bbu_status[i].cputemp, bbu_status[i].boardtemp, bbu_status[i].syncType, bbu_status[i].syncState, bbuState);

    }    
    ret = sprintf(buf, "%s", json_object_to_json_string(bbuState));
    json_object_put(bbuState);   
    return ret;
    
}

I32 cmd_gen_snf_result(I32 type, I8 * buf, I32 * data)
{
    I32 num = data[1];
    I32 i, off = 0;
    SNF_RSLT *result;
    time_t rawtime;
    time(&rawtime);

    JSON cmd_gen_snf_rlt, snf_string;

    cmd_gen_snf_rlt = json_object_new_object();
    snf_string = json_object_new_array();
   
    result = (SNF_RSLT *) (data + 2);

	for (i = 0; i < num; i++) {
        cli_app_log(CLI_LOG_INFO,"freq = %d ; pci = %d; tac = %d; rssi = %d priority = %d\n" ,result[i].freq, result[i].pci, result[i].tac, result[i]. rssi, result[i].priority);
		json_add_sniffer_array( data[0],result[i].tac, result[i].freq, result[i].rssi, result[i].pci, result[i].priority, snf_string);
	}

	json_object_object_add(cmd_gen_snf_rlt, "autoSnifferResult", snf_string);
	json_add_int(timestamp, rawtime,cmd_gen_snf_rlt);
	off = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_snf_rlt));
    cli_app_log(CLI_LOG_INFO, "BbuSniff result is %s \n", buf);
	json_object_put(cmd_gen_snf_rlt);
	cmd_gen_snf_rlt = NULL;

	return off;
	

}



static I32 cmd_handle_sync_system(I32 code, I8 * buf, I8 * rcvdata)
{
    I32 ret = IF_APP_ERROR;
    I32 len = IF_APP_ERROR;
    I8 data[DEV_INFO_ADD_LEN];
    memset(data, 0, DEV_INFO_ADD_LEN);
    I8 num[DEV_INFO_NUM_LEN];
    memset(num, 0, DEV_INFO_NUM_LEN);
    switch(code)
    {
        case SY_WHITELIST:
             len = cmd_get_num_par(buf, action, num);
             if (len > IF_APP_OK)
             {
                 len = cmd_get_char_par(buf, "imsiList", rcvdata);
                 if (len <= IF_APP_OK)
                     break;
                 if (atoi(num) == 1)
                 {                     
                     ret = IF_APP_ADD_WHITELIST ;   
                 } 
                 else
                     ret = IF_APP_DEL_WHITELIST ;
                 cli_app_log(CLI_LOG_INFO, "action is %d  imsiList %s ", atoi(num), rcvdata);
             } 
        break;
              


       default:
             ret =  IF_APP_ERROR;

    }
    return ret;
}

I32 cmd_check_is_rebootCmd(I8 *buf)
{
    I32 type = IF_APP_ERROR, code = IF_APP_ERROR;
    type = cmd_get_mng_hdr(buf, &code);
    if (type != IF_APP_ERROR)
    {
        if (code == CT_RESRART)
        {
            return IF_APP_OK;
        }
    }
    return IF_APP_ERROR;
}

I32 cmd_handle_mng_msg(I32 devType, I8 * buf, I32 length, I32 * data, I32 * fcode, I32 *cmd_type)
{
    I32 type, code;
    I32 ret = IF_APP_ERROR;
    I8 tmp[DEV_INFO_ADD_LEN * 2];
    bzero(tmp,( DEV_INFO_ADD_LEN * 2));

    type = cmd_get_mng_hdr(buf, &code);
    memset(data, 0, 8192);
    switch (type) {
        case CMD_CTRL:
            ret = cmd_handle_ctrl_msg(devType, code, buf, (I8 *) data);
            break;
        case CMD_CONFIG:
			
            cmd_get_char_par(buf, data_string, tmp);
            if (strlen(tmp) > 0) {
                ret = cmd_handle_conf_msg(devType, code, tmp, (I8 *) data);
                break;
            }
            ret = cmd_handle_conf_msg(devType, code, buf, (I8 *) data);
            break;
        case CMD_SEARCH:
			ret = cmd_handle_search_msg(code, buf, (I8 *)data);
            break;

        case CMD_SYNC:
            cmd_get_char_par(buf, data_string, tmp);
            if (strlen(tmp) > 0) {
                ret = cmd_handle_sync_system(code, tmp, (I8 *) data);
                break;
            }
            break;

        default:
            return IF_APP_ERROR;

    }
    *fcode = code;
    *cmd_type = type;
    return ret;
}

I32 packBlackList(I8 *buf,  PHONE_TMP * tmp)
{
    time_t rawtime ;
    time(&rawtime);
    PHONE_DATA *pdata;
    pdata = (PHONE_DATA *)(tmp->data);
    I8 data1[1024] = {0};
    I32 i = 0, num = 0, len1 = 0, ret = 0;
    num = tmp->num;
    for (; i < num; i++)
    {
        len1 += sprintf(data1 + len1, "#%s",(I8 *)pdata->imsi);
        pdata++;
    }
    JSON blackList;
    blackList = json_object_new_object();
    if (blackList == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, blackList);
    json_add_int("cmdType", CMD_SEARCH, blackList);
    json_add_int("code", SR_BLACKLIST, blackList);      
    json_add_string("data", data1, blackList);
    json_add_int(timestamp, rawtime, blackList);
    ret = sprintf(buf, "%s", json_object_to_json_string(blackList));
    json_object_put(blackList);
    return ret;
}

I32 cmd_gen_data_upload(UPDATA_CACHE * cache, PHONE_TMP * tmp, I32 tmp_off)
{
    I8 *buf;
    PHONE_DATA *pdata;
    JSON data_upload;
    enum json_tokener_error json_error;
    I32 i, len, num, off = tmp_off;
    num = tmp->num  ;
    if (num == 0)
        return 0;
    buf = cache->data;
    data_upload = json_tokener_parse_verbose(buf, &json_error);
    if ((json_error != 0) || (data_upload == NULL))
    {
        data_upload = json_object_new_array();
    }
    pdata = (PHONE_DATA *)(tmp->data + tmp_off);

    for (i=0; i < num; i++)
    {
        if (cache->num == MAX_DATA_PER_FRAME)
        {
            break;
        }
        if (cache->time < pdata->time_low)
        {
            cache->time = pdata->time_low;
        }
        json_add_data((I8 *)pdata->imei, (I8 *)pdata->imsi, (I8 *)pdata->tmsi, pdata->time_low, pdata->slotId, data_upload);
        cache->num++;
        tmp->num--;
        off += sizeof(PHONE_DATA);
        pdata++;
    }
    
    len = sprintf(buf, "%s", json_object_to_json_string(data_upload));
    cache->len = len;
    json_object_put(data_upload);
    return off;
}

I32 cmd_gen_data_upload_end(UPDATA_CACHE * cache, I8 *devNum)
{
    I8 *buf;
    JSON data_upload;
    enum json_tokener_error json_error;
    I32 len;

    buf = cache->data;
    data_upload = json_tokener_parse_verbose(buf, &json_error);
    JSON data_end;
    if ((json_error == 0))
    {
        data_end = json_object_new_object();
        json_add_string("deviceNumber", devNum, data_end);
        json_object_object_add(data_end, data_string, data_upload);
        len = sprintf(buf, "%s", json_object_to_json_string(data_end));
        cache->len = len;
        json_object_put(data_end);
    }    
    return len;

}

static I32 calc_the_distance(I32 rssi)
{
    float x;

    x = (rssi - 32.45 - 20 *log10(900) ) / 20;
    float distance;     
    distance = pow(10,x);  
    return ((I32) (distance * 1000));

}

I32 cmd_up_current_data(I8 *data, RECORDINFO *record)
{
    I32 i = 0, len = 0, rssi = 0;
    if (strcmp(dev_conf.dev.protocol, NANFEI) != 0)
    {
        JSON data_upload;
        data_upload = json_object_new_array();  
        if ( record != NULL )
        {
            cli_app_log(CLI_LOG_INFO, "send recordlist is %d \n", record->realNum);
            for ( i = 0; i < record->realNum ; i++)
            {
                json_add_data(record->phoneInfoBuf[i].imei, record->phoneInfoBuf[i].imsi, "", record->phoneInfoBuf[i].uptime, 
                        record->phoneInfoBuf[i].slotId, data_upload);        
            }

        }
        len = sprintf(data, "%s", json_object_to_json_string(data_upload));
        json_object_put(data_upload);
    }
    else
    {
        char szbuf[64] = {0};
        if (record != NULL)
        {
            for ( i = 0; i < record->realNum ; i++)
            {
                memset(szbuf, 0, 64);
                rssi = (73 + rand()%40);
                strftime(szbuf, 64, "%Y/%m/%d %H:%M:%S", localtime(((time_t *)(&record->phoneInfoBuf[i].uptime))));
                len += sprintf(data + len, "%s0000%02d%010d%s%15s%15s%04d%10s%10s%06d", dev_conf.dev.devNum, record->phoneInfoBuf[i].slotId>8?2:1, 
                i, szbuf, record->phoneInfoBuf[i].imsi, record->phoneInfoBuf[i].imei, rssi, dev_conf.dev.pos_lo, 
                dev_conf.dev.pos_la, calc_the_distance(rssi));
            }
        }
    }

    return len;    

}

I32 cmd_up_mac_current_data(I8 *data, MACRECORDINFO *record)
{
    I32 i = 0, len = 0;
    JSON data_upload;
    data_upload = json_object_new_array();  
    if ( record != NULL )
    {
        cli_app_log(CLI_LOG_INFO, "send mac recordlist is %d \n", record->realNum);
        for ( i = 0; i < record->realNum ; i++)
        {
            json_add_mac_data(record->macInfoBuf[i].mac, record->macInfoBuf[i].uptime, data_upload);        
        }
 
    }
    len = sprintf(data, "%s", json_object_to_json_string(data_upload));
    json_object_put(data_upload);

    return len;    
}

I32 pack_ftp_current_data(I8 *data, PHONE_TMP *tmp)
{
    I32 i = 0, len = 0, num = tmp->num;
    PHONE_DATA *pdata;
    pdata = (PHONE_DATA *)tmp->data;
    JSON data_upload;
    data_upload = json_object_new_array();  
    cli_app_log(CLI_LOG_INFO, "send phone num is %d \n", num);
    for ( i = 0; i < num ; i++)
    {
        json_add_data((I8 *)pdata->imei, (I8 *)pdata->imsi, "", pdata->time_low, 
                    pdata->slotId, data_upload);
        pdata++;        
    }
    len = sprintf(data, "%s", json_object_to_json_string(data_upload));
    json_object_put(data_upload);
    return len;    

}

I32 cmd_gen_sniff_result(I8 *buf, I8 *data)
{
    I32 len = 0;
    char result[8192]={0};
    sprintf(result, "%s", data);
    JSON cmd_gen_snf_rlt = json_object_new_object();
    json_add_int("result", RST_SUCESS, cmd_gen_snf_rlt);
    json_add_int("cmdType", CMD_CTRL, cmd_gen_snf_rlt);
    json_add_int("code", CT_SNIFFER, cmd_gen_snf_rlt); 
	json_add_string("msg", "", cmd_gen_snf_rlt);
    JSON data_upload;
    enum json_tokener_error json_error;
    data_upload = json_tokener_parse_verbose(data, &json_error);
    if (json_error != 0)
        data_upload = json_object_new_array();  
    json_object_object_add(cmd_gen_snf_rlt, data_string, data_upload);
    len = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_snf_rlt));
    json_object_put(cmd_gen_snf_rlt);

    return len;  

}

I32 cmd_gen_gsm_sniff_result(I8 *buf, I8 *data)
{
    I32 len = 0;
    char result[8192]={0};
    sprintf(result, "%s", data);
    JSON cmd_gen_snf_rlt = json_object_new_object();
	json_add_int("result", RST_SUCESS, cmd_gen_snf_rlt);
	json_add_string("msg", "Sniffer Complete", cmd_gen_snf_rlt);
    JSON data_upload;
    enum json_tokener_error json_error;
    data_upload = json_tokener_parse_verbose(data, &json_error);
    if (json_error != 0)
        data_upload = json_object_new_array();  
    json_object_object_add(cmd_gen_snf_rlt, data_string, data_upload);
    len = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_snf_rlt));
    json_object_put(cmd_gen_snf_rlt);

    return len;  
}

I32 cmd_handle_data_msg(I8 * buf, I32 length)
{
    //at this time only check if command is a 
    if (strstr(buf, upresult) != NULL) {
        return IF_APP_OK;
    }

    return IF_APP_ERROR;
}

I32 cmd_gen_warning(I32 code, I8 * buf, I8 * msg)
{
    I32 len;
    time_t rawtime;
    time(&rawtime);
    JSON cmd_gen_warn;
    cmd_gen_warn = json_object_new_object();
    json_add_int("wrType", code, cmd_gen_warn);
    json_add_string("msg", msg, cmd_gen_warn);
    json_add_int("uptime", rawtime, cmd_gen_warn);
    len = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_warn));
    json_object_put(cmd_gen_warn);
    cmd_gen_warn = NULL;

    return len;	
}

I32 cmd_gen_warning_with_data(I32 code, I8 * buf, I8 * data)
{
    I32 len;
    time_t rawtime;
    time(&rawtime);
    JSON cmd_gen_warn;
    cmd_gen_warn = json_object_new_object();
    json_add_int("wrType", code, cmd_gen_warn);
    json_add_string("msg", "", cmd_gen_warn);
    switch(code)
    {
        case WR_DEV_GPS:
            {
                GPSPOSITION *gpsinfo = (GPSPOSITION *)data;
                JSON gpsData;
                gpsData = json_object_new_object();
                json_add_double("longitude", gpsinfo->longitude, gpsData);
                json_add_double("latitude", gpsinfo->latitude, gpsData);
                json_add_double("altitude", gpsinfo->altitude, gpsData);
                json_object_object_add(cmd_gen_warn, "data", gpsData);
            }
            break;
        default:
            break;

    }
    json_add_int("uptime", rawtime, cmd_gen_warn);
    len = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_warn));
    json_object_put(cmd_gen_warn);
    cmd_gen_warn = NULL;

    return len;	
}

I32 cmd_gen_bbu_version(I8 *buf, I8 *version, I32 Bbu)
{
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_BBU_VERSION, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(Cell_Number, Bbu, dataRoot);
    json_add_string("version", version, dataRoot);
    json_object_object_add(cell_search, "data", dataRoot);
    json_add_int(timestamp, rawtime, cell_search);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
    json_object_put(cell_search);
    return ret;


}

I32 cmd_handle_register_msg(I8 * buf, I32 length)
{
    I32 len, code;
    I8 	num[DEV_INFO_NUM_LEN];
    //only 2 types message, 1st is welcome, 2nd is reg result.
    if(strncmp(buf, "Welecome", 8) == 0 || strncmp(buf, "Welcome", 7) == 0) {
        return IF_APP_WELCOME_MSG;
    }

    len = cmd_get_num_par(buf, code_string, num);
    if(len != IF_APP_ERROR) {
        cmd_convert_par(num, &code);
        if(code == SEX_REG_SUCCESS) {

            return IF_APP_REG_SUCCESS;
        }

        return IF_APP_REG_FAILURE;
    }

    return IF_APP_ERROR;
}

I32 cmd_hanldle_device_requestMsg(I32 type, I32 code, I8 *buf)
{
    I32 len = 0;
    JSON requestMsg;
    requestMsg = json_object_new_object();
    json_add_int("ReqType", type, requestMsg);
    json_add_int("ReqCode", code, requestMsg);
    len = sprintf(buf, "%s", json_object_to_json_string(requestMsg));
    json_object_put(requestMsg);
    return len;	
}

I32  cmd_gen_blackList(I8 *buf, PHONEINFO *phone)
{
    I32 len = 0;
    JSON requestMsg;
    requestMsg = json_object_new_object();
    json_add_int("ReqType", CMD_OTHERS, requestMsg);
    json_add_int("ReqCode", BLACKLIST_ALERT, requestMsg);
    json_add_string("imsi", phone->imsi, requestMsg);
    len = sprintf(buf, "%s", json_object_to_json_string(requestMsg));
    json_object_put(requestMsg);
    return len;	
    
}

I32 cmd_gen_delay_report(I8 *buf, DELAY_REPORT *delay_report)
{
    I32 len = 0;
    JSON dReport;
    dReport = json_object_new_object();
    I32 i = 0;
    for (i = 0; i < 6; i++)
    {
        if (strlen(imsilist[i]) > 4 && strcmp(imsilist[i], delay_report->imsi) == 0)
            break;
        if (strlen(imsilist[i]) < 4 && !isRoot)
        {
            strcpy(imsilist[i], delay_report->imsi);
            updateRecord(delay_report->imsi);
            break;
        }
    }
    json_add_string(imsi_s, delay_report->imsi, dReport);

    json_add_int("delay", delay_report->delay, dReport);
 
    json_add_int("sinr", delay_report->sinr, dReport);
    json_add_int("freq", delay_report->freq, dReport);
    json_add_int("BBU", delay_report->slotId, dReport);
    json_add_int("rsrp", delay_report->rsrp, dReport);
    json_add_int("pci", delay_report->pci, dReport);
    len = sprintf(buf, "%s", json_object_to_json_string(dReport));
    json_object_put(dReport);
    return len;

}

I32 cmd_gen_device_usage(U32 *currentTm, U32 *startTm, U32 *endTm, I32 *len, I8 *buf, I8 *data)
{   
    int ret = IF_APP_OK;
    time_t rawtime;
    time(&rawtime);
    if (*startTm == 0)
    {
        SR_DATE date = *(SR_DATE *)data;
        struct tm tm = {0};
        if (strptime(date.startDate, "%Y-%m-%d", &tm) == NULL)
            ret = IF_APP_ERROR;
        *startTm = mktime(&tm);
        if (*startTm < 0)
            ret =  IF_APP_ERROR;

        if (strptime(date.endDate, "%Y-%m-%d", &tm) == NULL)
            ret = IF_APP_ERROR;
        *endTm = mktime(&tm);
        if (*endTm < 0)
            ret = IF_APP_ERROR;
        *endTm += 86399;
        
    } 
    if (ret == IF_APP_ERROR)
    {
        JSON cmd_gen_mng_res;

        cmd_gen_mng_res = json_object_new_object();
		json_add_int("result", RST_FAILED, cmd_gen_mng_res);
        json_add_int("cmdType", CMD_SEARCH, cmd_gen_mng_res);
        json_add_int("code", SR_DEVICE_USAGE, cmd_gen_mng_res);
		json_add_string("msg", "", cmd_gen_mng_res);
		json_add_int(timestamp, rawtime, cmd_gen_mng_res);
		*len = sprintf(buf, "%s", json_object_to_json_string(cmd_gen_mng_res));
        json_object_put(cmd_gen_mng_res);
        return ret;

    }
    USERRECORD user;
    memset(&user, 0, sizeof(USERRECORD));
    user.num = 0;
    cli_app_log(CLI_LOG_INFO, "current tm is %d  endTM %d \n", *currentTm, *endTm);
    packrecordInStruct(currentTm, startTm, *endTm, &user);
    JSON userInfo;
    userInfo = json_object_new_array();
    int i = 0;
    for (i = 0; i < user.num; i++)
    {
        JSON singleUser = json_object_new_object();
        json_add_int("time", user.userTm[i], singleUser);
        json_add_string("targetImsi", user.targetImsi[i], singleUser);
        json_add_string("catchImsi", user.catchImsi[i], singleUser);
        json_add_string("userName", user.userName[i],singleUser);
        json_add_string("userId", user.userId[i], singleUser);
        json_object_array_add(userInfo, singleUser);
    }
    JSON info = json_object_new_object();
    json_add_int("result", RST_SUCESS, info);
    json_add_int("cmdType", CMD_SEARCH, info);
    json_add_int("code", SR_DEVICE_USAGE, info);
    json_add_string("msg", "", info);
    json_object_object_add(info, "data", userInfo);
 	*len = sprintf(buf, "%s", json_object_to_json_string(info));
    
	json_object_put(info);
   
    if (user.num < 10)
        return IF_APP_ERROR;
    else
        return IF_APP_OK;

}


void cmd_gen_ftp_devstatus(I8 *Mac, I8 *buf)
{
    JSON requestMsg;
    requestMsg = json_object_new_object();
    I8 devNum[128] = {0};
    sprintf(devNum, "%s%s", dev_conf.ftp.ftpcompany, Mac);
    json_add_string("fc", dev_conf.ftp.ftpcompany, requestMsg);
    json_add_string("sc", dev_conf.ftp.ftpbsic, requestMsg);
    json_add_string("dc", devNum, requestMsg);
    json_add_int("ds", 0, requestMsg);
    sprintf(buf, "%s", json_object_to_json_string(requestMsg));
    json_object_put(requestMsg);
    return ;	
}

void cmd_gen_ftp_sitestatus(I8 *buf)
{
    JSON requestMsg;
    requestMsg = json_object_new_object();
    json_add_string("fc", dev_conf.ftp.ftpcompany, requestMsg);
    json_add_string("sc", dev_conf.ftp.ftpbsic, requestMsg);
    json_add_int("ss", 0, requestMsg);
    sprintf(buf, "%s", json_object_to_json_string(requestMsg));
    json_object_put(requestMsg);
    return ;	

}

I32 cmd_gen_hw_id(I8 *buf)
{
    JSON cell_search, dataRoot  ;  
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
    dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_HW_ID, cell_search);      
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);
    json_add_string("hardware_id", dev_conf.dev.devNumRsv, dataRoot);
    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
	//cli_app_log(CLI_LOG_NOTICE, "buf is %s \n", buf);
    json_object_put(cell_search);
    return ret;
}

I32 cmd_gen_server_restart_snf(DEV_CONF *dev_conf, I8 *buf)
{
    JSON cell_search, dataRoot, restartInfo, serverInfo, snfInfo, snfInfo1;
    time_t rawtime ;
    I32 ret = IF_APP_ERROR;
    time(&rawtime);
    cell_search = json_object_new_object();
	dataRoot = json_object_new_object();
    if (cell_search == NULL)
        return IF_APP_ERROR;
	
    json_add_int("result", RST_SUCESS, cell_search);
    json_add_int("cmdType", CMD_SEARCH, cell_search);
    json_add_int("code", SR_SERVER_RESART_SNF, cell_search);
    json_add_string("msg", "", cell_search);
    json_add_int(timestamp, rawtime, cell_search);

    restartInfo = json_object_new_object();
    json_add_int("enable", atoi(dev_conf->restart.enable), restartInfo);
    json_add_int("hour", atoi(dev_conf->restart.hour), restartInfo);
    json_add_int("minute", atoi(dev_conf->restart.min), restartInfo);
    json_object_object_add(dataRoot, "restartCfg", restartInfo);

    serverInfo = json_object_new_object();
    json_add_int("mgr1Port", atoi(dev_conf->mng.port), serverInfo);
    json_add_string("mgr1SrvHost", dev_conf->mng.addr, serverInfo);
    json_add_int("up1Port", atoi(dev_conf->data.port), serverInfo);
    json_add_string("up1SrvHost", dev_conf->data.addr, serverInfo);
    json_object_object_add(dataRoot, "serverInfo", serverInfo);

    snfInfo = json_object_new_object();
	snfInfo1 = json_object_new_object();
    json_add_int("autoBccMode", atoi(dev_conf->snfGsm.bccMode), snfInfo);
    json_add_bool("isAuto", atoi(dev_conf->snfGsm.isAuto), snfInfo);
    json_add_int("hour", atoi(dev_conf->snfGsm.hour), snfInfo1);
    json_add_int("minute", atoi(dev_conf->snfGsm.min), snfInfo1);
    json_add_int("second", atoi(dev_conf->snfGsm.sec), snfInfo1);
    json_object_object_add(snfInfo, "runTime", snfInfo1);
    json_add_int("snifferCycle", atoi(dev_conf->snfGsm.cycle), snfInfo);    
    json_object_object_add(dataRoot, "snfInfo", snfInfo);

    json_object_object_add(cell_search, "data", dataRoot);
    ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
	//cli_app_log(CLI_LOG_NOTICE, "buf is %s \n", buf);
    json_object_put(cell_search);
	
    return ret;
}

I32 cmd_gen_boardcard_info(DEV_CONF *dev_conf, I8 *data, I8 *buf)
{
	JSON cell_search, dataRoot, gsmcfg, redetectTime, att, snfInfo, snfInfo1, rfswitch;
	time_t rawtime;
	I32 slotId = atoi(data);
	I32 ret = IF_APP_ERROR;
	time(&rawtime);
	cell_search = json_object_new_object();
	dataRoot = json_object_new_object();
	if(cell_search == NULL)
		return IF_APP_ERROR;
	
	json_add_int("result", RST_SUCESS, cell_search);
	json_add_int("cmdType", CMD_SEARCH, cell_search);
	json_add_int("code", SR_BOARDCARD_INFO, cell_search);
	json_add_string("msg", "", cell_search);
	json_add_int(timestamp, rawtime, cell_search);
	    
	gsmcfg = json_object_new_object();
	if(slotId == 8)
		gen_gsm_config(slotId, dev_conf->capGsm.gsm1, gsmcfg);
	else if(slotId == 9)
		gen_gsm_config(slotId, dev_conf->capGsm.gsm2, gsmcfg);
	json_object_object_add(dataRoot, "gsmCfg", gsmcfg);
	
	redetectTime = json_object_new_object();
	if(slotId == 8 || slotId == 9)
		json_add_int("reDetectMinuts", atoi(dev_conf->capGsm.intvl), redetectTime);
	json_object_object_add(dataRoot, "reDetectTime", redetectTime);

	att = json_object_new_object();
	if(slotId == 8)
		json_add_int("upatt", atoi(dev_conf->capGsm.gsm1.upatt), att);
	else if(slotId == 9)
		json_add_int("upatt", atoi(dev_conf->capGsm.gsm2.upatt), att);
	json_object_object_add(dataRoot, "upAtt", att);
	
	snfInfo = json_object_new_object();
	snfInfo1 = json_object_new_object();
	json_add_int("autoBccMode", atoi(dev_conf->snfGsm.bccMode), snfInfo);
	json_add_bool("isAuto", atoi(dev_conf->snfGsm.isAuto), snfInfo);
	json_add_int("hour", atoi(dev_conf->snfGsm.hour), snfInfo1);
	json_add_int("minute", atoi(dev_conf->snfGsm.min), snfInfo1);
	json_add_int("second", atoi(dev_conf->snfGsm.sec), snfInfo1);
	json_object_object_add(snfInfo, "runTime", snfInfo1);
	json_add_int("snifferCycle", atoi(dev_conf->snfGsm.cycle), snfInfo);    
	json_object_object_add(dataRoot, "snfInfo", snfInfo);
	
	rfswitch = json_object_new_object();
	if(slotId == 8)
		json_add_int("rfSwitch", atoi(dev_conf->capGsm.gsm1.enable), rfswitch);
	else if(slotId == 9)
		json_add_int("rfSwitch", atoi(dev_conf->capGsm.gsm2.enable), rfswitch);
	json_object_object_add(dataRoot, "RfSwitch", rfswitch);
	
	json_object_object_add(cell_search, "data", dataRoot);
	ret = sprintf(buf, "%s", json_object_to_json_string(cell_search));
	cli_app_log(CLI_LOG_NOTICE, "buf is %s \n", buf);
	json_object_put(cell_search);
	
    return ret;
}



